Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/twitter to image #20

Merged
merged 2 commits into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.