import json from os.path import join, dirname from os import environ from watson_developer_cloud import VisualRecognitionV3 as VisualRecognition import picamera import subprocess from time import sleep import os import zipfile visual_recognition = VisualRecognition( '2016-05-20', api_key='dec041de3393d4b66120d22e628b81f2f3c1e9c0') with open(join(dirname(__file__), 'surya.zip'), 'rb') as surya, \ open(join(dirname(__file__), 'vijay.zip'), 'rb') as vijay: print( json.dumps(visual_recognition.create_classifier( 'face', surya_positive_examples=surya, vijay_positive_examples=vijay), indent=2))
import json from os.path import join, dirname from os import environ from watson_developer_cloud import VisualRecognitionV3 as VisualRecognition from watson_developer_cloud import VisualRecognitionV3 as VisualRecognition #31e589a7ae4990a6261e68d8b2931acf5145b973 - alvaro - junto #37ff1e95c9da3f5e5161d6f49b0139469c087f8d . watsonR - separados visual_recognition = VisualRecognition('2016-05-20', api_key='31e589a7ae4990a6261e68d8b2931acf5145b973') with open(join(dirname(__file__), 'positive_todas.zip'), 'rb') as carne_uptc, \ open(join(dirname(__file__), 'negative_arregladas.zip'), 'rb') as carne_otro: print(json.dumps(visual_recognition.create_classifier('carne_uptc', completo_positive_examples=carne_uptc, negative_examples=carne_otro), indent=2)) #with open(join(dirname(__file__), 'positive_arregladas.zip'), 'rb') as carne_completo, \ # open(join(dirname(__file__), 'positive_arregladas.zip'), 'rb') as carne_cortado, \ # open(join(dirname(__file__), 'negative_arregladas.zip'), 'rb') as carne_otro: # print(json.dumps(visual_recognition.create_classifier('CarnesUPTCvsCarnesOtros', completo_positive_examples=carne_completo, cortado_positive_examples=carne_cortado, negative_examples=carne_otro), indent=2))
class CustomClassifier(): def __init__(self): self.visual_recognition = VisualRecognition('2016-05-20', api_key=config.api_key) self.downloader = ImageDownloader(config.yoox_rest_endpoint) """ This method creates a new visual custom classifier starting from a file path. The path will be a directory containing a group of zip files filled with jpg images, each file represents a class sample data @type classifierName: string @param classifierName: new custom classifier name @type inputDataPath: string @param inputDataPath: zip files directory path """ def create_custom_classifier(self, classifierName, inputDataPath): # it could take a negative examples data path as input params = {} for dirname, subdirs, files in os.walk(inputDataPath): for file in files: absname = os.path.abspath(os.path.join(dirname, file)) name = os.path.splitext(os.path.basename(file))[0] params[name.replace("-", "_") + "_positive_examples"] = open(absname) if config.print_file_params_long_object: print params # for dirname, subdirs, files in os.walk(negDataPath): # for file in files: # absname = os.path.abspath(os.path.join(dirname, file)) # name = os.path.splitext(os.path.basename(file))[0] # params["negative_examples"] = open(absname) print "Uploading files to Visual Recognition servers... It will take several time based on the number of images" response = self.visual_recognition.create_classifier(classifierName, **params) print "Watson classifier created: ", json.dumps(response) self.classifier_id = response['classifier_id'] def sameIdCondition(self, id, inputID): return id == inputID """ This method retrieves a classifier by id @type classifier_id: string @param classifier_id: classifier id @return response: Watson's representation of a classifier """ def getClassifiers(self, classifier_id): response = self.visual_recognition.get_classifier(classifier_id=classifier_id) return response """ This method retrieves this classifier representation @return response: Watson's representation of a classifier """ def getClassifierStatus(self): return self.getClassifiers(self.classifier_id) """ This method recharge a Watson visual recognition classifier by id @type classifier_id: string @param classifier_id: classifier id """ def recharge_classifier(self, classifier_id): # if the classifier does not exist it will fail self.classifier_id = self.getClassifiers(classifier_id)['classifier_id'] """ This method requests to the classifier for an image classification starting from an image ID of the csv file @type imageId: string @param imageId: an image ID contained in the csv training set file @return: the visual recognition representation of the requested image classification """ def classify_image_fromTrainingSet(self, imageId, threshold): # Read the CSV File allImages = pre_processing.readingFromInputFile(config.csv_path) # in a production env it should be necessary to handle the not found exception imgEl = [el for el in allImages if imageId == el.id][0] print config.temp_data_path if not os.path.isdir(config.temp_data_path): os.makedirs(config.temp_data_path) # Download the image imgPath = self.downloader.downloadAndSaveImage(config.temp_data_path, imgEl) print "Sending classification request to classifier" return self.visual_recognition.classify(open(imgPath), classifier_ids=[self.classifier_id], threshold=threshold) """ This method requests to the classifier for an image classification starting from its ID @type imageId: string @param imageId: an image ID contained in the csv training set file @return: the visual recognition representation of the requested image classification """ def classify_image(self, imageId, threshold): imgPath = self.downloader.downloadAndSaveImage(config.temp_data_path, InputImgElement(imageId)) print "Sending classification request to classifier" return self.visual_recognition.classify(open(imgPath), classifier_ids=[self.classifier_id], threshold=threshold)