Skip to content

Commit

Permalink
Merge pull request #20 from shortintern2020-A-labyrinth/feature/Twitt…
Browse files Browse the repository at this point in the history
…er_to_image

Feature/twitter to image
  • Loading branch information
nawta authored Aug 25, 2020
2 parents ff262a5 + 2d23f4a commit c38de63
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public class ImageController {
public String test(
@RequestParam(name = "img_path", defaultValue = "/Users/NakamuraTsukasa/Desktop/633.png") String img_path
) throws FileNotFoundException {
// String url = "https://zukan.pokemon.co.jp/zukan-api/up/images/index/94b9fb82b38847b83a8041e9a78989ce.png";
String url = "https://zukan.pokemon.co.jp/zukan-api/up/images/index/94b9fb82b38847b83a8041e9a78989ce.png";

IamAuthenticator authenticator = new IamAuthenticator("8nwN0eKp7eZ73So4DLdPndp_yv-vtlI27pN1wK2TjVg1");
VisualRecognition visualRecognition = new VisualRecognition("2019-07-12", authenticator);

ClassifyOptions options = new ClassifyOptions.Builder()
//.url(url)
// .url(url)
.imagesFile(new File(img_path))
.build();
ClassifiedImages result = visualRecognition.classify(options).execute().getResult();
Expand Down
62 changes: 55 additions & 7 deletions src/main/java/com/example/demo/controller/ViewController.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.example.demo.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ibm.cloud.sdk.core.security.IamAuthenticator;
import com.ibm.watson.natural_language_understanding.v1.NaturalLanguageUnderstanding;
import com.ibm.watson.natural_language_understanding.v1.model.*;
import com.ibm.watson.visual_recognition.v3.VisualRecognition;
import com.ibm.watson.visual_recognition.v3.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -38,7 +43,7 @@ public String read(@PathVariable("id") String id) {
}

//TODO: Controllerのままか、返り値の型の調整どうするか考える(フロントとの兼ね合い。)
@RequestMapping(path = "/twitter_to_NLU", method = RequestMethod.GET)
@RequestMapping(path = "/twitter_NLU_keyword", method = RequestMethod.GET)
public List<String> collect_NLU_keywords_from_tweets(
@RequestParam(name = "username", defaultValue = "CNN") String username,
@RequestParam(name = "tweet_id", defaultValue = "1000000") long tweet_id
Expand All @@ -49,12 +54,39 @@ public List<String> collect_NLU_keywords_from_tweets(
for (Status status : result.getTweets()) {
String tmp = NLU(status.getText());
if(tmp != null) {
System.out.println(tmp);
// System.out.println(tmp);
NLU_results.add(tmp);
}
}

return NLU_results;
//TODO: 整形する必要あるかも?
}


@RequestMapping(path = "/twitter_image_keyword", method = RequestMethod.GET)
public List<String> collect_image_keywords_from_tweets(
@RequestParam(name = "username", defaultValue = "CNN") String username,
@RequestParam(name = "tweet_id", defaultValue = "1000000") long tweet_id
) throws Exception {
QueryResult result = search_user(username, tweet_id);
ArrayList<String> imageprocessing_results = new ArrayList<>();

for (Status status : result.getTweets()) {
if(status.getMediaEntities().length > 0) {
// System.out.println(status.getMediaEntities()[0].getMediaURL());

String tmp = ImageProcessing(status.getMediaEntities()[0].getMediaURL());
if (tmp != null) {
// System.out.println(tmp);
imageprocessing_results.add(tmp);
}
}
}

return imageprocessing_results;
// ["call center", "people", "newsreader"]みたいなんがくる。

}


Expand All @@ -71,9 +103,7 @@ private QueryResult search_user(String username, long tweet_id) throws TwitterEx
query.setQuery("from:" + username);
query.setSinceId(tweet_id);

QueryResult result = twitter.search(query);

return result;
return twitter.search(query);
}

private String NLU(String text) {
Expand Down Expand Up @@ -105,12 +135,30 @@ private String NLU(String text) {
resultCategories.add(splitword[splitword.length - 1]);
}
}


// return resultCategories.toString();
return response.getCategories().get(0).getLabel();
}


private String ImageProcessing(String url) throws FileNotFoundException {

IamAuthenticator authenticator = new IamAuthenticator("8nwN0eKp7eZ73So4DLdPndp_yv-vtlI27pN1wK2TjVg1");
VisualRecognition visualRecognition = new VisualRecognition("2019-07-12", authenticator);

ClassifyOptions options = new ClassifyOptions.Builder()
.url(url)
.build();
ClassifiedImages result = visualRecognition.classify(options).execute().getResult();
System.out.println(result);

List<ClassifiedImage> images = result.getImages();

ClassifiedImage image = images.get(0);
ClassifierResult r = image.getClassifiers().get(0);
ClassResult c = r.getClasses().get(0);

return c.getXClass();

}

}
Binary file modified target/classes/com/example/demo/controller/ViewController.class
Binary file not shown.

0 comments on commit c38de63

Please sign in to comment.