def loadModel(): prediction = CustomImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath("model200.h5") prediction.setJsonPath("class1.json") prediction.loadModel(num_objects=12) return prediction
def __init__(self, image_path, model_path, output_path): self.avgColor = [] self.pcaColors = [] self.detector = ObjectDetection() self.detector.setModelTypeAsYOLOv3() self.detector.setModelPath(model_path + "yolo.h5") self.detector.loadModel(detection_speed="flash") self.prediction = CustomImagePrediction() self.prediction.setModelTypeAsResNet() #self.prediction.setModelPath(model_path + "model_ex-012_acc-0.988819.h5") self.prediction.setModelPath(model_path + "model_ex-043_acc-0.996787.h5") self.prediction.setJsonPath(model_path + "model_class.json") self.prediction.loadModel(num_objects=2) now = time.localtime() self.frame_folder = str(now.tm_year) + str(now.tm_mon) + str(now.tm_mday) self.image_path = image_path self.output_path = output_path + "detection/" + self.frame_folder + "/" if os.path.exists(image_path) is False: os.mkdir(image_path) if os.path.exists(output_path) is False: os.mkdir(output_path) if os.path.exists(os.path.join(output_path, 'detection')) is False: os.mkdir(os.path.join(output_path, 'detection')) if os.path.exists(os.path.join(output_path, 'processed')) is False: os.mkdir(os.path.join(output_path, 'processed')) self.create_output_folder()
def fileUpload(): target = os.path.join(UPLOAD_FOLDER, 'test_docs') if not os.path.isdir(target): os.mkdir(target) logger.info("welcome to upload`") file = request.files['file'] filename = secure_filename(file.filename) destination = "/".join([target, filename]) file.save(destination) session['uploadFilePath'] = destination execution_path = os.getcwd() dir = os.path.dirname(os.path.realpath(__file__)) dir.replace("\\", "\\\\") prediction = CustomImagePrediction() prediction.setModelTypeAsResNet() prediction.setJsonPath(dir + r"\model_class.json") prediction.setModelPath(dir + r"\model_ex-085_acc-0.966261.h5") prediction.loadModel(num_objects=2) try: predictions, probabilities = prediction.predictImage( dir + r"\\test_docs\\" + filename, result_count=2) except: tf.keras.backend.clear_session() os.remove('test_docs\\' + filename) return "error" tf.keras.backend.clear_session() p1, p2 = zip(predictions, probabilities) result = p1[0] os.remove('test_docs\\' + filename) return result
class Color(object): prediction = CustomImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath("./model_data/best_color.h5") prediction.setJsonPath("./model_data/color_class.json") prediction.loadModel(num_objects=2) def color_API(path): name = [] pro = [] predictions, probabilities = Color.prediction.predictImage( path, result_count=2) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction, ':', eachProbability) name.append(eachPrediction) pro.append(eachProbability) if (pro[0] > pro[1]): Name = name[0] else: Name = name[1] # print(Name) if (Name == '0'): return 0 else: return 1
def __init__(self): self.prediction = CustomImagePrediction() self.prediction.setModelTypeAsResNet() self.prediction.setModelPath( "datasets/models/model_ex-062_acc-0.916385.h5") self.prediction.setJsonPath("datasets/json/model_class.json") self.prediction.loadModel(num_objects=2) self.webcam = Webcam()
class Color(object): prediction = CustomImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath("./model_data/best_color.h5") prediction.setJsonPath("./model_data/color_class.json") prediction.loadModel(num_objects=2) def color_API(path): # name=[] # pro=[] # predictions, probabilities = Color.prediction.predictImage(path, result_count=2) # for eachPrediction, eachProbability in zip(predictions, probabilities): # print(eachPrediction,':',eachProbability) # name.append(eachPrediction) # pro.append(eachProbability) # if(pro[0]>pro[1]): # Name=name[0] # else: # Name=name[1] # # print(Name) # if(Name=='0'): # return 0 # else: # return 1 i = 1 for fileName in fileList: #遍历文件夹中所有文件 name = [] pro = [] n = len(fileList) print('本次需要检测', n, '张照片') k = n - i start = timer() print('还剩', k, '张待检测照片名称', fileName) Color.prediction.loadModel(num_objects=2) predictions, probabilities = Color.prediction.predictImage( ('./input/' + str(fileName)), result_count=2) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction, ':', eachProbability) name.append(eachPrediction) pro.append(eachProbability) i = i + 1 if (pro[0] > pro[1]): Name = name[0] else: Name = name[1] print(Name) # if(Name=='0'): ## img=cv.imread('./input/'+fileName) ## cv.imwrite('./0/'+fileName,img) # else: ## img=cv.imread('./input/'+fileName) ## cv.imwrite('./1/'+fileName,img) # Name=name[1] end = timer() print('单张照片执行时间', end - start) print('finish')
def run_predict(): predictor = CustomImagePrediction() predictor.setModelPath(model_path="trafficnet_resnet_model_ex-055_acc-0.913750.h5") predictor.setJsonPath(model_json="model_class.json") predictor.loadFullModel(num_objects=4) predictions, probabilities = predictor.predictImage(image_input="", result_count=4) #PATH FOR IMAGE for prediction, probability in zip(predictions, probabilities): print(prediction, " : ", probability)
def run_predict(): predictor = CustomImagePrediction() predictor.setModelPath(model_path="trafficnetmodelacc-0.893750.h5") predictor.setJsonPath(model_json="model_class.json") predictor.loadFullModel(num_objects=4) predictions, probabilities = predictor.predictImage(image_input="images/dense.jpg", result_count=4) for prediction, probability in zip(predictions, probabilities): print(prediction, " : ", probability)
def detect_move_from_webcam( self, model_file_name=None, config_file_name="model_class.json", webcam_index=0, sensibility=80, ): # Instantiate the CustomImagePrediction object that will predict our moves predictor = CustomImagePrediction() # Set the model type of the neural network (it must be the same of the training) self._set_proper_model_type(self.model_type, predictor) if not model_file_name: # If model file name is not set, ask user to choose which model must be used model_file_name = self._get_model_file_name(self.dataset_name) # Set path to the trained model file predictor.setModelPath( os.path.join(self.base_path, self.dataset_name, "models", model_file_name)) # Set path to the json file that contains our classes and their labels predictor.setJsonPath( os.path.join(self.base_path, self.dataset_name, "json", config_file_name)) # Load the trained model and set it to use "class_number" classes predictor.loadModel(num_objects=self.class_number) # Context manager that help us to activate/deactivate our webcam with opencv_video_capture(webcam_index) as cap: # Run until user press "q" key while True: # Acquire a frame from webcam _, frame = cap.read() # Do a prediction on that frame predictions, probabilities = predictor.predictImage( frame, result_count=3, input_type="array") # Get a tuple (class_predicted, probability) that contains the best # prediction best_prediction = max(zip(predictions, probabilities), key=lambda x: x[1]) # If probability of the best prediction is >= of the min sensibility # required, it writes a label with predicted class name and probability # over the frame if best_prediction[1] >= sensibility: cv2.putText( frame, f"{best_prediction[0]} ({round(best_prediction[1], 2)})", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA, ) # Display the acquired frame on a dedicated window cv2.imshow("Move predictor", frame) # Break cycle if user press "q" key if cv2.waitKey(1) & 0xFF == ord("q"): break
def ImageML(): prediction = CustomImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath("idenprof/models/idenprof_061-0.7933.h5") prediction.setJsonPath("idenprof/json/model_class.json") prediction.loadModel(num_objects=10) predictions, probabilities = prediction.predictImage("image.jpg", result_count=3) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction, " : ", eachProbability)
def run_predict(): predictor = CustomImagePrediction() predictor.setModelPath(model_path="action_net_ex-060_acc-0.745313.h5") predictor.setJsonPath(model_json="model_class.json") predictor.loadFullModel(num_objects=16) predictions, probabilities = predictor.predictImage( image_input="images/5.jpg", result_count=4) for prediction, probability in zip(predictions, probabilities): print(prediction, " : ", probability)
def getModel(): global prediction if prediction is not None: return prediction prediction = CustomImagePrediction() prediction.setModelTypeAsResNet50() prediction.setModelPath("model.h5") prediction.setJsonPath("styles.json") prediction.loadModel(num_objects=25) return prediction
def ImageClassifier(Image, ModelFile, JsonFile, Classes): execution_path = os.getcwd() #get model path prediction = CustomImagePrediction( ) #create instance for the prediction model prediction.setModelTypeAsResNet() #set classification model to ResNet prediction.setModelPath(ModelFile) #loads best model trained prediction.setJsonPath(JsonFile) #loads best model trained prediction.loadModel(num_objects=Classes) #Loads best model trained predictions, probabilities = prediction.predictImage( Image, result_count=3) #classify the image return predictions, probabilities
def classify_trash(images): prediction = CustomImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath(os.path.join(execution_path, "model.h5")) prediction.setJsonPath(os.path.join(execution_path, "model_class.json")) prediction.loadModel(num_objects=6) trashTypes = set() for i in images: pre, prob = prediction.predictImage(i, input_type="array") trashTypes.add(pre[0]) return trashTypes
def getOccupation(filename): prediction = CustomImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath("preTrainedModels/idenprof.h5") prediction.setJsonPath("preTrainedModels/idenprof.json") prediction.loadModel(num_objects=10) predictions, probabilities = prediction.predictImage(filename, result_count=1) return predictions[0]
def ObjectDetect(self): execution_path = "C:\Tensorflow\models\Research\object_detection\Engine\customPrediction" print(execution_path) detector = ObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath( os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5")) detector.loadModel() detections1 = detector.detectCustomObjectsFromImage( input_image=os.path.join(execution_path, "trail1.jpg"), output_image_path=os.path.join(execution_path, "example3.jpg")) detector2 = ObjectDetection() detector2.setModelTypeAsYOLOv3() detector2.setModelPath(os.path.join(execution_path, "yolo.h5")) detector2.loadModel() detections2 = detector2.detectCustomObjectsFromImage( input_image=os.path.join(execution_path, "trail1.jpg"), output_image_path=os.path.join(execution_path, "example4.jpg")) detector3 = ObjectDetection() detector3.setModelTypeAsTinyYOLOv3() detector3.setModelPath(os.path.join(execution_path, "yolo-tiny.h5")) detector3.loadModel() detections3 = detector3.detectCustomObjectsFromImage( input_image=os.path.join(execution_path, "trail1.jpg"), output_image_path=os.path.join(execution_path, "example5.jpg")) prediction = CustomImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath( os.path.join(execution_path, "model_ex-027_acc-0.843750.h5")) prediction.setJsonPath(os.path.join(execution_path, "model_class.json")) prediction.loadModel(num_objects=2) predictions, probabilities = prediction.predictImage(os.path.join( execution_path, "trail1.jpg"), result_count=5) detections = detections1 + detections2 + detections3 List = [] for i in detections: List.append(i["name"]) for eachPrediction, eachProbability in zip(predictions, probabilities): if eachProbability > 50: List.append(eachPrediction) """for eachObject in detections: print(eachObject["name"], " : ", eachObject["percentage_probability"]) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction, " : ", eachProbability)""" return List
def detect(): prediction = CustomImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath("model200.h5") prediction.setJsonPath("class1.json") prediction.loadModel(num_objects=12) predictions, probabilities = prediction.predictImage("C:/xampp/htdocs/tcs/temp.jpg", result_count=1) for eachPrediction, eachProbability in zip(predictions, probabilities): item = eachPrediction return item
def main(path_model, path_classes, path_image): prediction = CustomImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath(path_model) prediction.setJsonPath(path_classes) prediction.loadModel(num_objects=10) predictions, probabilities = prediction.predictImage(path_image, result_count=5) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction, " : ", eachProbability)
def test_custom_recognition_full_model_resnet(): predictor = CustomImagePrediction() predictor.setModelPath(os.path.join(main_folder, "data-models", "idenprof_full_resnet_ex-001_acc-0.119792.h5")) predictor.setJsonPath(model_json=os.path.join(main_folder, "data-json", "idenprof.json")) predictor.loadFullModel(num_objects=10) predictions, probabilities = predictor.predictImage(image_input=os.path.join(main_folder, main_folder, "data-images", "9.jpg")) assert isinstance(predictions, list) assert isinstance(probabilities, list) assert isinstance(predictions[0], str) assert isinstance(probabilities[0], str)
def foo(): a=[] prediction= CustomImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath("model_ex-030_acc-0.996974.h5") prediction.setJsonPath("model_class2.json") prediction.loadModel(num_objects=13) predictions, probabilities = prediction.predictImage("images/image.jpg", result_count=3) for eachPrediction, eachProbability in zip(predictions, probabilities): #print(eachPrediction , " : " , eachProbability) a.append(eachPrediction) return jsonify({"Your image result is here and solution has been sent via a mail": a[0]})
def match_tracks(detections, video_name, thresh=10, scale=4): matched = set() scores = dict() frame_num = 0 cap = cv2.VideoCapture(video_name) prediction = CustomImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath("models/resnet_model.h5") prediction.setJsonPath("models/resnet_model_class.json") prediction.loadModel(num_objects=2) a = 0 while True: ret, frame = cap.read() if not ret: break if frame_num in detections: for ID in detections[frame_num]: if ID in matched: continue try: box = scaleROI(detections[frame_num][ID], scale) img = frame[box[1]:box[3], box[0]:box[2]] cv2.imwrite("temp.1.jpg", img) predictions, probabilities = prediction.predictImage( "temp.1.jpg", result_count=2) boat_score = probabilities[0] if predictions[ 0] == 'boat' else probabilities[1] if boat_score >= 65: a += 1 print(predictions, probabilities, a) cv2.imshow("", img) cv2.waitKey(1) scores[ID] = scores[ID] + 1 if ID in scores else 1 if scores[ID] >= thresh: matched.add(ID) except Exception as e: pass frame_num += 1 tracks = dict({-1: dict()}) for frame_num in detections: for ID in detections[frame_num]: if ID not in tracks: tracks[ID] = dict() if ID in matched: tracks[-1][frame_num] = detections[frame_num][ID] else: if len(tracks[ID]) >= 3 * 20: tracks[ID][frame_num] = detections[frame_num][ID] return tracks
def test_custom_recognition_model_densenet(): predictor = CustomImagePrediction() predictor.setModelTypeAsDenseNet() predictor.setModelPath(os.path.join(main_folder, "data-models", "idenprof_densenet-0.763500.h5")) predictor.setJsonPath(model_json=os.path.join(main_folder, "data-json", "idenprof.json")) predictor.loadModel(num_objects=10) predictions, probabilities = predictor.predictImage(image_input=os.path.join(main_folder, main_folder, "data-images", "9.jpg")) assert isinstance(predictions, list) assert isinstance(probabilities, list) assert isinstance(predictions[0], str) assert isinstance(probabilities[0], str)
def loadPrediction(self, prediction_speed='normal', num_objects=10): if self.__modelloaded == False: if self.__modelType == "": raise ValueError( "You must set a valid model type before loading the model." ) if self.__jsonPath == "": raise ValueError( "You must set a valid json path before loading the model." ) elif self.__modelType == "resnet": prediction = CustomImagePrediction() prediction.setModelTypeAsResNet() elif self.__modelType == "squeezenet": prediction = CustomImagePrediction() prediction.setModelTypeAsSqueezeNet() elif self.__modelType == "densenet": prediction = CustomImagePrediction() prediction.setModelTypeAsDenseNet() elif self.__modelType == "inceptionv3": prediction = CustomImagePrediction() prediction.setModelTypeAsInceptionV3() elif self.__modelType == "vgg": prediction = CustomImagePrediction() prediction.setModelTypeAsVgg() prediction.setModelPath(self.modelPath) prediction.setJsonPath(self.__jsonPath) prediction.loadModel(prediction_speed, num_objects) self.__prediction_collection.append(prediction) self.__modelloaded = True else: raise ValueError( "You must set a valid model type before loading the model.")
def detect(): prediction = CustomImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath(os.path.join("model200.h5")) prediction.setJsonPath("class1.json") prediction.loadModel(num_objects=14) predictions, probabilities = prediction.predictImage( "UPLOAD_FOLDER/photo.jpg", result_count=1) # for eachPrediction, eachProbability in zip(predictions, probabilities): # print(eachPrediction) # item = eachPrediction item = database(predictions[0]) return item
def init(): #Prepare and load model global execution_path, prediction, graph, session execution_path = os.getcwd() session = tf.Session() graph = tf.get_default_graph() tf.keras.backend.set_session(session) prediction = CustomImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath( execution_path + "/Train&Test/chest_xray/models/model_ex-003_acc-0.773026.h5") prediction.setJsonPath(execution_path + "/Train&Test/chest_xray/json/model_class.json") prediction.loadModel(num_objects=2)
def agricheck(): prediction = CustomImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath(r"C:\Users\Kunal Jindal\PycharmProjects\agricure.tech\Files\model_ex-022_acc-0.966484.h5") prediction.setJsonPath(r"C:\Users\Kunal Jindal\PycharmProjects\agricure.tech\Files\model_class.json") prediction.loadModel(num_objects=18) for a, b, c in os.walk(r"C:\Users\Kunal Jindal\PycharmProjects\agricure.tech\static\upload"): imglist = a + r"\\" + c[0] predictions, probabilities = prediction.predictImage(imglist, result_count=1) os.remove(imglist) db = TinyDB(r"C:\Users\Kunal Jindal\PycharmProjects\agricure.tech\Files\db.json") User = Query() val = (db.search(User.code == predictions[0]))[0] outlist = {'code': predictions[0], 'plant': val["plant"], 'treatment': val["treatment"], 'disease': val["disease"]} return outlist
async def main(file_type: str, files: str): model = CustomImagePrediction() model.setModelTypeAsResNet() model.setModelPath( os.path.join(EXECUTION_PATH, 'data/images/models/kermit_finder.h5')) model.setJsonPath( os.path.join(EXECUTION_PATH, 'data/images/json/model_class.json')) model.loadModel(num_objects=2) # number of objects on your trained model if file_type == 'image': for image in files.split(','): print(await predict_image(image_name=image, model=model)) else: await predict_video(video_path=files, model=model)
def activity_detector(image_name): result = [] predictor = CustomImagePrediction() predictor.setModelPath( model_path=os.path.join(root, "action-detection-image", "action.h5")) predictor.setJsonPath(model_json=os.path.join( root, "action-detection-image", "model_class.json")) predictor.loadFullModel(num_objects=16) predictions, probabilities = predictor.predictImage(image_input=image_name, result_count=4) for prediction, probability in zip(predictions, probabilities): result.append([prediction, probability]) print(result) return result[0][0]
def __init__(self, yolo_weigths='../../models_h5/yolo.h5', model_weigths='../../models_h5/model_ex-055_acc-0.996250.h5', model_json='../../models_h5/model_class.json'): self.detector = ObjectDetection() self.detector.setModelTypeAsYOLOv3() self.detector.setModelPath(yolo_weigths) self.detector.loadModel() self.classifier = CustomImagePrediction() self.classifier.setModelTypeAsResNet() self.classifier.setModelPath(model_weigths) self.classifier.setJsonPath(model_json) self.classifier.loadModel(num_objects=2) self.current_detection = []
def obj_prediction(img): execution_path = os.getcwd() prediction = CustomImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath(os.path.join(execution_path, "rat_model.h5")) prediction.setJsonPath(os.path.join(execution_path, "model_class.json")) prediction.loadModel(num_objects=2, prediction_speed="fastest") predictions, probabilities = prediction.predictImage(os.path.join( execution_path, img + ".png"), result_count=2) max_prediction = "" max_probability = -1.00 print("\n=================================") for eachPrediction, eachProbability in zip(predictions, probabilities): if (max_probability < float(eachProbability)): max_prediction = eachPrediction max_probability = float(eachProbability) print(eachPrediction + " : " + eachProbability) print("=================================\n") # create Image object with the input image image = Image.open(img + ".png") # initialise the drawing context with the image object as background draw = ImageDraw.Draw(image) # create font object with the font file and specify desired size font = ImageFont.truetype("Roboto-Black.ttf", size=size, encoding="unic") colour = "rgb(255, 255, 255)" # draw the message on the background draw.rectangle(((0, 0), (1500, 160)), outline="rgb(0, 0, 0)", fill="rgb(0, 0, 0)") draw.text((x, y), max_prediction + ": " + str(max_probability), fill=colour, font=font) # save the edited image image.save(img + "_pro.png") print("Generated " + img_name + "_pro.png")