def detec(self, modelo, velocidad): #deteccion de manzanas execution_path = os.getcwd() #obtiene el directorio detector = ObjectDetection() #crea un objeto if (modelo == 1): #elige el modelo detector.setModelTypeAsYOLOv3() #carga el tipo de modelo detector.setModelPath(os.path.join( execution_path, "yolo.h5")) #carga el archivo con el modelo de deteccion else: detector.setModelTypeAsRetinaNet() #carga el tipo de modelo detector.setModelPath( os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5") ) #carga el archivo con el modelo de deteccion detector.loadModel( velocidad ) #carga modelo y se puede ajustar la velocidad de esta, si se aumenta hace una deteccion menos precisa detections = detector.detectObjectsFromImage( input_image=os.path.join(execution_path, self.nombre_archivo), output_image_path=os.path.join(execution_path, "imagenew.jpg"), display_percentage_probability=True, display_object_name=False, minimum_percentage_probability=40) self.det = detections #para el numero de objetos for eachObject in detections: if (eachObject["name"] != 'apple'): self.obj_sosp = True
class MyDetection: def __init__(self): pass def setDetection(self): execution_path = os.getcwd() self.detector = ObjectDetection() self.detector.setModelTypeAsYOLOv3() self.detector.setModelPath(os.path.join(execution_path, "yolo.h5")) self.detector.loadModel(detection_speed="normal") self.detector.detectObjectsFromImage( input_image=os.path.join(execution_path, "t.jpg"), output_image_path=os.path.join(execution_path, "image3new.jpg"), minimum_percentage_probability=30) def detectionImage(self, imageName): res = self.detector.detectObjectsFromImage( input_image=imageName, minimum_percentage_probability=30) a = list() for i in res: t = dict() t['name'] = i['name'] t['box_points'] = list() t['box_points'].append(int(i['box_points'][0])) t['box_points'].append(int(i['box_points'][1])) t['box_points'].append(int(i['box_points'][2])) t['box_points'].append(int(i['box_points'][3])) a.append(t) ret = dict() ret['filename'] = imageName ret['data'] = a print(ret) return json.dumps(ret)
def trigger_camera(n_clicks): if n_clicks == 0: return '' print('trigger camera %d' % n_clicks) pwd = os.environ['PWD'] camera_image = '%s/camera.jpeg' % pwd camera_ai_image = '%s/camera_ai.jpeg' % pwd function.camera().takephoto(saveas=camera_image) global detector if detector is None: if not os.path.exists('yolo.h5'): os.system( 'wget https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/yolo.h5' ) execution_path = os.getcwd() detector = ObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath(os.path.join(execution_path, "yolo.h5")) detector.loadModel() if detector is None: encoded_image = base64.b64encode(open(camera_image, 'rb').read()).decode() else: detections = detector.detectObjectsFromImage( input_image=camera_image, output_image_path=camera_ai_image, minimum_percentage_probability=30) encoded_image = base64.b64encode(open(camera_ai_image, 'rb').read()).decode() print('finish camera') return 'data:image/jpeg;base64,%s' % encoded_image
def getPhotoObject(self): os.chdir('..') execution_path = os.getcwd() detector = ObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath(os.path.join(execution_path, "yolo.h5")) detector.loadModel() media_path = self.navigateToMediaDir() photos = {} for name in os.listdir('.'): with open(name, "r") as read_file: data = json.load(read_file) for key in data['photos']: detection_types = self.getObjectTypes( detector, media_path, key['uri']) geolocation = GeoLocation() geo_loc = geolocation.getLongLatFromIP( str(key['media_metadata']['photo_metadata'] ['upload_ip'])) photos[str(key['uri']).split('/')[2]] = { 'category': data['name'], 'uri': str(key['uri']).split('/')[2], 'geo': geo_loc, 'detection_types': detection_types } return photos
def body_detection(file): detector = ObjectDetection() # set the model type # 1. Retina net # 2. Yolov3 (currently used) # 3. Yolo-tiny detector.setModelTypeAsYOLOv3() # provide the location of the h5 file detector.setModelPath(os.getcwd() + "/static/models/yolov3/yolo.h5") # load the model detector.loadModel() # 80 objects can be detected, but only focusing on Human Bodies custom = detector.CustomObjects(person=True) detections, extracted_objects = detector.detectCustomObjectsFromImage( custom_objects=custom, input_image=file, output_image_path=os.getcwd() + "/static" "/op_images/" + file, display_object_name=False, display_percentage_probability=False, extract_detected_objects=True, minimum_percentage_probability=30) return detections, extracted_objects
class CameraCheck: _instance = None def __new__(cls, *args, **kwargs): if not CameraCheck._instance: CameraCheck._instance = super(CameraCheck, cls).__new__(cls, *args, **kwargs) return CameraCheck._instance def __init__(self): self.detector = ObjectDetection() # self.detector.setModelTypeAsTinyYOLOv3() # self.detector.setModelPath("yolo-tiny.h5") self.detector.setModelTypeAsYOLOv3() self.detector.setModelPath("yolo.h5") self.detector.loadModel() self.custom = self.detector.CustomObjects(person=True) def check(self, image_name): execution_path = os.getcwd() detections = self.detector.detectCustomObjectsFromImage( custom_objects=self.custom, input_image=os.path.join(execution_path, image_name), output_image_path=os.path.join(execution_path, "detected_people.jpg"), minimum_percentage_probability=20) return len(detections)
def getYolo(request): if request.method == "POST": try: f = request.FILES['sentFile'] # here you get the files needed except: return render(request, 'homepage.html') execution_path = os.getcwd() detector = ObjectDetection() path = os.path.join(settings.MODELS, "yolo.h5") detector.setModelTypeAsYOLOv3() detector.setModelPath(path) detector.loadModel() detections = detector.detectObjectsFromImage(input_image=f, output_image_path='media/imagetest.jpg') item = [] object = '' for eachObject in detections: item.append(eachObject["name"]) if len(item)==0: return render(request, 'failPredictions.html') categoryName = objectCategory.objects.filter(object__in=item).values_list('categoryName', flat=True) ac = AdvertisementCategory.objects.filter(id__in=categoryName) ads = Advertisement.objects.filter(categoryName__in=ac) if len(ads)==0: return render(request, 'failPredictions.html') context = { 'ac': ac, 'ads': ads, } return render(request, 'predictions.html', context) else: return render(request, 'homepage.html')
def detect (input, output_path): #Detection function images = get_images(input) #input image if not path.isdir(output_path): print("not a valid directory, creating it") #If the path is not a valid directory, it will create it makedirs(output_path) detector = ObjectDetection() #Call the ObjectDetection method as a variable model_dir = path.join(".", "models") #New directory that combines the path with .models model_path = path.join(model_dir,"yolo.h5") #Takes new directory and attaches it to yolo.h5 if not path.isfile(model_path): from download_model import download_model #Download model if it is not valid download_model() t0 = time() detector.setModelTypeAsYOLOv3() #Call these 3 Model methods for detection detector.setModelPath(model_path) detector.loadModel() t1 = time() print("Model Loaded", t1-t0) found = 1 min_probability = 60 #Minimum probability is set to 60 for image in images: find = scan_image( image, path.join(output_path,f"dog{found}.jpg"), min_probability, detector ) if find: found += 1 #Add 1 to found number each time of positive result print("Completed", time()-t1)
class Detector(): def __init__(self): ## Set up detector self.detector = ObjectDetection() # Load model self.detector.setModelTypeAsRetinaNet() self.detector.setModelTypeAsYOLOv3() model_path = 'resnet50_coco_best_v2.0.1.h5' self.detector.setModelPath(os.path.join(os.getcwd(), model_path)) self.detector.loadModel(detection_speed='fast') # Set custom object detection for dogs # For more options, see: https://github.com/OlafenwaMoses/ImageAI/tree/master/imageai/Detection#---custom-object-detection self.custom_objects = self.detector.CustomObjects(dog=True) def detect(self, input_path, output_path): # Detect detections = self.detector.detectCustomObjectsFromImage( input_image=input_path, output_image_path=output_path, custom_objects=self.custom_objects, minimum_percentage_probability=45) bounding_boxes = [] for d in detections: bounding_boxes.append(d['box_points']) return bounding_boxes
def processImage(input_file, output_file): os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' execution_path = os.getcwd() # input_file = sys.argv[1] # output_file = sys.argv[2] detector = ObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath(os.path.join(execution_path, "yolo.h5")) detector.loadModel() custom_objects = detector.CustomObjects(bottle=True, apple=True, orange=True, sandwich=True, hot_dog=True, pizza=True, donut=True, cake=True) detections = detector.detectCustomObjectsFromImage( custom_objects=custom_objects, input_image=os.path.join(execution_path, input_file), output_image_path=os.path.join(execution_path, output_file), minimum_percentage_probability=30) totalPrice = 0 for eachObject in detections: totalPrice = totalPrice + prices[eachObject["name"]] # print(eachObject["name"] , " : ", eachObject["percentage_probability"], " : ", eachObject["box_points"] , " : " , prices[eachObject["name"]] , " Baht" ) # print("================================") return (totalPrice, output_file)
def load_model(model_path, detection_speed): detector = ObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath(model_path) detector.loadModel(detection_speed=detection_speed) return detector
def init_detector(): detector = ObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath('D:/_assets/models/yolo.h5') detector.loadModel() custom_objects = detector.CustomObjects(person=True) return detector, custom_objects
class ImageaiDetector(object): def __init__(self, input_file, output_file, model_path, detection_speed="fast"): self.input_file = input_file self.output_file = output_file self.model_path = model_path self.detector = ObjectDetection() self.detector.setModelTypeAsYOLOv3() self.detector.setModelPath(self.model_path) self.detector.loadModel(detection_speed=detection_speed) def detect_objects(self, input_image, output_image=""): input_image_path = os.path.join(self.input_file, input_image) if not os.path.exists(input_image_path): return [] return self.detector.detectObjectsFromImage( input_image=input_image_path, output_image_path=os.path.join( self.output_file, output_image if output_image else input_image), minimum_percentage_probability=90)
def obj_detect(): if request.method == 'POST': from imageai.Detection import ObjectDetection model = ObjectDetection() model.setModelTypeAsYOLOv3() model.setModelPath("./models/yolo.h5") model.loadModel() detections, paths = model.detectObjectsFromImage( input_image=image_to_process, output_image_path=OUTPUT_PATH, extract_detected_objects=True) img_to_render = os.path.join('..', image_to_process) img_to_render = img_to_render.replace('\\', '/') paths = [os.path.join('..', path) for path in paths] paths = [path.replace('\\', '/') for path in paths] for i, path in enumerate(paths): detections[i]['index'] = i detections[i]['path'] = path for detection in detections: detection['percentage_probability'] = round( detection['percentage_probability'], 2) return render_template('result.html', img_name=img_to_render, img_details=detections, num_obj=len(detections))
def init_detector(model_path): detector = ObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath(model_path) detector.loadModel() return detector
def yolov3(input_path:str) -> dict: """runs yolov3 on image Arguments: input_path {str} -- [path to local image] load up the yolov3 model pass it the input file path and output file path get hash and upload to s3 extract yolov3 results into the return dictionary Returns: [dict] -- [dictionary of predictions] """ detector = ObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath(weights_path) detector.loadModel() detection = detector.detectObjectsFromImage(input_image=input_path, output_image_path=temp_output_path) data = dict() with open(temp_output_path, 'rb') as outfile, open(input_path, 'rb') as infile: filename= hashlib.md5(infile.read()).hexdigest() + '_yolov3.png' data['url'] = upload_file_to_s3(outfile, config('S3_BUCKET'), filename) for item in detection: data[item["name"]] = str(item["percentage_probability"]) return data
class Detector(): def __init__(self): ## Set up detector self.detector = ObjectDetection() # Load model self.detector.setModelTypeAsYOLOv3() #self.detector.setModelTypeAsRetinaNet() model_name = 'pretrained-yolov3.h5' #SET TO NAME OF DESIRED MODEL OF USE model_path = os.path.join(os.getcwd(), model_name) self.detector.setModelPath(os.path.join(os.getcwd(), model_path)) self.detector.loadModel() # Set custom object detection for dogs # For more options, see: https://github.com/OlafenwaMoses/ImageAI/tree/master/imageai/Detection#---custom-object-detection self.custom_objects = self.detector.CustomObjects(dog=True) def detect(self, img): # Detect ''' _ , detections = self.detector.detectCustomObjectsFromImage(input_type = "array", input_image= img, output_type = "array", custom_objects=self.custom_objects, minimum_percentage_probability=80) ''' _, detections = self.detector.detectCustomObjectsFromImage( input_image=img, custom_objects=self.custom_objects, minimum_percentage_probability=80) bounding_boxes = [] for d in detections: bounding_boxes.append(d['box_points']) return bounding_boxes
def check_people(img_path): detector = ObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath('yolo.h5') detector.loadModel() peopleOnly = detector.CustomObjects(person=True) detectedImage, detections = detector.detectCustomObjectsFromImage(custom_objects=peopleOnly, output_type="array", input_image=img_path, minimum_percentage_probability=30) return len(detections)
def load_model(): global execution_path execution_path = os.getcwd() global detector detector = ObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath( os.path.join(execution_path , "yolo.h5")) detector.loadModel()
def detection(output_file): # keyframe file for detection keyframe_file = os.path.join(output_file, "keyframe") # save detection results file for keyframe output_full_image_file = os.path.join(output_file, "result_keyframe") if not os.path.exists(output_full_image_file): os.makedirs(output_full_image_file) # save detection results file for cropped person output_crop_person_file = os.path.join(output_file, "result_crop") if not os.path.exists(output_crop_person_file): os.makedirs(output_crop_person_file) # detection detector = ObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath(os.path.join(execution_path, "yolo.h5")) detector.loadModel() videos_list = os.listdir(keyframe_file) videos_list.sort() for video in videos_list: # mkdir files for result output_video_keyframe_file = os.path.join(output_full_image_file, video) if not os.path.exists(output_video_keyframe_file): os.makedirs(output_video_keyframe_file) output_video_crop_file = os.path.join(output_crop_person_file, video) if not os.path.exists(output_video_crop_file): os.makedirs(output_video_crop_file) # read keyframe and detect video_keyframe_dir = os.path.join(keyframe_file, video) keyframes_list = os.listdir(video_keyframe_dir) keyframes_list.sort() for keyframe in keyframes_list: keyframe_dir = os.path.join(video_keyframe_dir, keyframe) res_keyframe_dir = os.path.join(output_video_keyframe_file, keyframe) # detect and save results for keyframes custom_objects = detector.CustomObjects(person=True) detections = detector.detectCustomObjectsFromImage( custom_objects=custom_objects, input_image=keyframe_dir, output_image_path=res_keyframe_dir) ori_image = cv2.imread(keyframe_dir) count = 1 for eachObject in detections: if eachObject["name"] == 'person': bb = eachObject['box_points'] person_image = ori_image[bb[1]:bb[3], bb[0]:bb[2]] person_image_dir = os.path.join( output_video_crop_file, keyframe[0:-4] + "_" + str(count).zfill(2) + ".jpg") cv2.imwrite(person_image_dir, person_image) count += 1
class main(): def __init__(self): self.detector = ObjectDetection() self.model_path = "static/yolo.h5" #actual path self.detector.setModelTypeAsYOLOv3() self.detector.setModelPath(self.model_path) self.detector.loadModel()
class Model: def __init__(self): self.execution_path = os.getcwd() self.detector = ObjectDetection() self.detector.setModelTypeAsYOLOv3() self.detector.setModelPath( os.path.join(self.execution_path, "tripsyModel/yolo.h5")) self.detector.loadModel() print("Model Loaded SuccessFully!")
def detectsetting(): print('hi~3') detector = ObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath("/home/wndvlf96/abandog/yolo.h5") detector.loadModel() custom_objects = detector.CustomObjects(dog=True) print('Cropsetting completed') return detector, custom_objects
def process_frame(): execution_path = os.getcwd() detector = ObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath(os.path.join(execution_path, "yolo.h5")) detector.loadModel() detections = detector.detectObjectsFromImage( input_image=os.path.join(execution_path, "captured-image.jpg"), output_image_path=os.path.join(execution_path, "processed-image.jpg"), minimum_percentage_probability=50)
def load_detector(): detector = ObjectDetection() detector.setModelTypeAsYOLOv3() fine_tuned_model_path = os.path.join(manage.logs_path, "000", "trained_weights_final.h5") detector.setModelPath(fine_tuned_model_path) detector.loadModel( ) #"normal"(default), "fast", "faster" , "fastest" and "flash" return detector
class main(): """ This class is defined to instantiate ObjectDetection class and load Yolov3 model through the constructor. """ def __init__(self): self.detector = ObjectDetection() #creating object of Object Detection class. self.model_path = "static/yolo.h5" #specifying path of yolo model. self.detector.setModelTypeAsYOLOv3() #selecting model type self.detector.setModelPath(self.model_path) #setting model path self.detector.loadModel()#loading Yolo model
def initialize(): global detector LOGGER.info("Creating Object Detector") detector = ObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath(model) LOGGER.info("Object Detector created") LOGGER.info("Loading model: %s", model) detector.loadModel() LOGGER.info("Model loaded")
def load_detector(model_type, model_path, detection_speed="normal"): detector = ObjectDetection() if model_type == ModelType.YOLO: detector.setModelTypeAsYOLOv3() elif model_type == ModelType.YOLO_TINY: detector.setModelTypeAsTinyYOLOv3() elif model_type == ModelType.RES_NET: detector.setModelTypeAsRetinaNet() detector.setModelPath(model_path) detector.loadModel(detection_speed=detection_speed) return detector
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
class Detection: def __init__(self): self.path = os.getcwd() self.detector = VideoObjectDetection() self.detector.setModelTypeAsYOLOv3() self.detector.setModelPath("ObjectDetectionTensorflow/yolo.h5") self.detector.loadModel(detection_speed="fast") self.imgDetector = ObjectDetection() self.imgDetector.setModelTypeAsYOLOv3() self.imgDetector.setModelPath("ObjectDetectionTensorflow/yolo.h5") self.imgDetector.loadModel() self.camera = cv2.VideoCapture(0) def liveVideo(self): #Live to Video videoPath = self.detector.detectObjectsFromVideo( camera_input=self.camera, output_file_path=os.path.join(self.path, "Loaded_Video"), frames_per_second=30, log_progress=True, minimum_percentage_probability=80) print(videoPath) cv2.imshow('video', videoPath) def liveVideoShow(self): #Live video detection while True: ret, frame = self.camera.read() img = PIL.Image.fromarray(frame) img.save("ObjectDetectionTensorflow/images/pic.png") detected = self.imgDetector.detectCustomObjectsFromImage( input_image="ObjectDetectionTensorflow/images/pic.png", output_image_path="ObjectDetectionTensorflow/images/pic.png", minimum_percentage_probability=40) for eachObject in detected: print(eachObject["name"], " : ", eachObject["percentage_probability"], " : ", eachObject["box_points"]) print("--------------------------------") if cv2.waitKey(33) == ord('a'): break img = mpimg.imread("ObjectDetectionTensorflow/images/pic.png") cv2.imshow('video', img)