This repository was archived by the owner on Oct 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathimage_keywords.pl
More file actions
62 lines (49 loc) · 1.87 KB
/
image_keywords.pl
File metadata and controls
62 lines (49 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/perl -w
############################################################################
#
# AlchemyAPI Perl Example: Image Keyword Extraction
# Author: AlchemyAPI, LLC
# Copyright (C) 2009-2014, AlchemyAPI, LLC.
#
############################################################################
use strict;
use AlchemyAPI;
# Create the AlchemyAPI object.
my $alchemyObj = new AlchemyAPI();
# Load the API key from disk.
if ($alchemyObj->LoadKey("api_key.txt") eq "error")
{
die "Error loading API key. Edit api_key.txt and insert your API key.";
}
my $result = '';
my $imageParams = new AlchemyAPI_ImageKeywordParams();
# Get a list of keywords for an image
my $imageURL = "http://betting-strategy.org/wp-content/uploads/2013/08/Horse-racing-1.jpg";
$result = $alchemyObj->URLGetRankedImageKeywords($imageURL, $imageParams);
printf $result;
# Let's try giving the URL call an HTML page, as it should
# extract the most relevant image before providing relevant keywords
$imageParams->SetExtractMode("trust-metadata");
$result = $alchemyObj->URLGetRankedImageKeywords("http://www.cnn.com/", $imageParams);
printf $result;
# Change the extractMode parameter
$imageParams->SetExtractMode("always-infer");
$result = $alchemyObj->URLGetRankedImageKeywords("http://www.cnn.com/", $imageParams);
printf $result;
# Change the extractMode parameter
$imageParams->SetExtractMode("only-metadata");
$result = $alchemyObj->URLGetRankedImageKeywords("http://www.cnn.com/", $imageParams);
printf $result;
# Now, let's try using the POST-based approach
# use a sample image
my $localImage = "data/beach_wedding_at_dusk.jpg";
open IMG, $localImage or die $!;
binmode IMG;
my ($buffer, $data, $length);
while (($length = read IMG, $data, 4) != 0) {
$buffer .= $data;
}
close(IMG);
$imageParams->SetImagePostMode("raw");
$result = $alchemyObj->ImageGetRankedImageKeywords($buffer, $imageParams);
printf $result