with graph.as_default(): video_path = detector.detectCustomObjectsFromVideo( custom_objects=custom, input_file_path=file_path, output_file_path= os.path.join(execution_path ,"Detected {}".format(os.path.basename(file_path[:-4]))), frames_per_second=29, minimum_percentage_probability=min_prob, log_progress=True) else: with graph.as_default(): video_path = detector.detectObjectsFromVideo(input_file_path=file_path, output_file_path=os.path.join(execution_path ,"Detected {}".format(os.path.basename(file_path[:-4]))), minimum_percentage_probability=min_prob, frames_per_second=29, log_progress=True) #video_path = detector.detectObjectsFromVideo(input_file_path=file_path, # output_file_path=os.path.join(execution_path,"Detected {}".format(os.path.basename(file_path[:-4]))), # frames_per_second=29, log_progress=True) return(video_path) detector1 = ObjectDetection() detector1.setModelTypeAsYOLOv3() detector1.setModelPath("yolo.h5") detector1.loadModel() graph1 = tf.get_default_graph() result=tuple() listt=[] prob={} def ImageDetection(file_path,objects=None,min_prob=30): print(file_path) execution_path = r'./media/output_video' #detector = ObjectDetection() #detector.setModelTypeAsYOLOv3() #detector.setModelPath("yolo.h5") #detector.loadModel() if objects:
def analyzeImages(consumer, producer, frameDetails): """ Start analyzing images """ print("Start analyzing images...") detector = ObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath("yolo.h5") detector.loadModel() try: frameReference = 0 totalAnalysisTime = 0 while True: for msg in consumer: parent = msg.value frameReference += 1 byteStream = io.BytesIO(parent.image) originalTime = parent.dateTime image = np.asarray(bytearray(byteStream.read()), dtype="uint8") image = cv2.imdecode(image, cv2.IMREAD_UNCHANGED) beforeDetection = time.process_time() imageai_frame, detection = detector.detectObjectsFromImage( input_image=image, input_type="array", output_type="array") detectionTime = time.process_time() - beforeDetection totalAnalysisTime += detectionTime imageTime = datetime.datetime.now() print("--------------------------------") identified_objects = [] print(imageTime) for eachObject in detection: print( eachObject["name"], " : ", eachObject["percentage_probability"], " : ", eachObject["box_points"], ) identified_objects.append({ "name": eachObject["name"], "percentage_probability": round(eachObject["percentage_probability"], 2), "position": [ int(eachObject["box_points"][0]), int(eachObject["box_points"][1]), int(eachObject["box_points"][2]), int(eachObject["box_points"][3]), ], }) print("--------------------------------\n\r") # Convert image to png ret, buffer = cv2.imencode(".jpg", imageai_frame) # Convert to bytes and send to kafka frameDetails.setChildFrame( frameReference, buffer.tobytes(), identified_objects, parent, round(detectionTime, 2), round(totalAnalysisTime / frameReference, 2), ) producer.send(frameDetails.topic, frameDetails) except Exception as e: traceback.print_exc() print("\nExiting.") sys.exit(1)
def get(self, request, projectId, imageId, format=None): try: project = Project.objects.get(pk=projectId) image = Image.objects.get(pk=imageId) except project.DoesNotExist or image.DoesNotExist: return HttpResponse(status=404) # print(project) # print(image) labels = Label.objects.filter(project=projectId) # print(labels) # print(project.label_type) if project.label_type == 1: currDir = os.getcwd() detector = ObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath( os.path.join(currDir, "image/image_auto/yolo.h5")) detector.loadModel() detections = detector.detectObjectsFromImage( input_image=os.path.join(currDir, "media/{}".format(image.path)), output_image_path=os.path.join(currDir, "out.jpg"), minimum_percentage_probability=30) for eachObject in detections: for label in labels: if label.label_name.lower() == eachObject["name"].lower(): box = eachObject["box_points"] coordinate = [] coordinate.append({ "lng": box[0], "lat": box[1], }) coordinate.append({ "lng": box[2], "lat": box[3], }) shape = Shape(label=label, image=image, coordinate=coordinate) shape.save() jsonData = {} label_data = {} for label in labels: label_data[label.id] = [] shapes = Shape.objects.filter(image=imageId) type = "bbox" if project.label_type == 1 else "polygon" for shape in shapes: points = shape.coordinate points = points.replace("\'", "\"") point_datas = json.loads(points) points = [{ "lng": point['lng'], "lat": point['lat'], } for point in point_datas] label_data[shape.label.id].append({ "id": str(shape.id), "type": type, "color": "red", "points": points, "tracingOptions": { "trace": '', "enable": False, "smoothing": 1 }, "formId": 1 }) image = { "id": str(imageId), "originalName": str(image.name), "link": 'http://localhost:8000/media/{}'.format(image.path), "externalLink": '', "localPath": '', "labeled": str(image.completed), "labelData": { "labels": label_data, } } forms = [] for label in labels: forms.append({ "id": str(label.id), "type": type, "name": str(label.label_name) }) jsonData['project'] = ProjectSerializer(project, context={ 'request': self.request }).data jsonData['project']['forms'] = forms jsonData['image'] = image return JsonResponse(jsonData)
class Camera(object): detector = ObjectDetection() custom_objects = detector.CustomObjects(car=True, person=True, bus=True, chair=True, truck=True, refrigerator=True, oven=True, bicycle=True, skateboard=True, train=True, bench=True, motorcycle=True, bed=True, suitcase=True) def __init__(self, index=0): self.cap = cv2.VideoCapture(index) self.openni = index in (cv2.CAP_OPENNI, cv2.CAP_OPENNI2) self.fps = 0 execution_path = os.getcwd() self.detector.setModelTypeAsYOLOv3() self.detector.setModelPath(os.path.join(execution_path , "yolo.h5")) self.detector.loadModel(detection_speed="fastest") print ("Model Loaded") def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): self.release() def release(self): if not self.cap: return self.cap.release() self.cap = None def capture(self, callback, gray=False): if not self.cap: sys.exit('The capture is not ready') while True: t = cv2.getTickCount() if self.openni: if not self.cap.grab(): sys.exit('Grabs the next frame failed') ret, depth = self.cap.retrieve(cv2.CAP_OPENNI_DEPTH_MAP) # ret, frame = self.cap.retrieve(cv2.CAP_OPENNI_GRAY_IMAGE ret, frame = self.cap.retrieve(cv2.CAP_OPENNI_DEPTH_MAP if gray else cv2.CAP_OPENNI_BGR_IMAGE) detections = self.detector.detectCustomObjectsFromImage( custom_objects=self.custom_objects, input_type="array", input_image=frame, output_type="array", minimum_percentage_probability=20)[0] if callback: callback(detections, depth, self.fps) else: ret, frame = self.cap.read() detections = self.detector.detectCustomObjectsFromImage( custom_objects=self.custom_objects, input_type="array", input_image=frame, output_type="array", minimum_percentage_probability=20)[0] if not ret: sys.exit('Reads the next frame failed') if gray: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) if callback: callback(detections, self.fps) t = cv2.getTickCount() - t self.fps = cv2.getTickFrequency() / t # esc, q ch = cv2.waitKey(10) & 0xFF if ch == 27 or ch == ord('q'): break def fps(self): return self.fps def get(self, prop_id): return self.cap.get(prop_id) def set(self, prop_id, value): self.cap.set(prop_id, value)
class VideoCamera(object): execution_path = os.getcwd() detector = ObjectDetection() detector.setModelTypeAsTinyYOLOv3() detector.setModelPath(os.path.join(execution_path , "yolo-tiny.h5")) detector.loadModel(detection_speed="flash") #buffer All_faces = [0,0,0,0,0,0] def __init__(self): self.video = cv2.VideoCapture(0) def __del__(self): self.video.release() #queue def second(self,name): self.All_faces[self.All_faces.index(name)] = 0 #text-to-speech def speak(self,detections): for eachObject in detections: name = eachObject["name"] if name not in self.All_faces: if (self.All_faces[0] != 0): for i in range(5,0,-1): self.All_faces[i]=self.All_faces[i-1] self.All_faces[0]=name timer = threading.Timer(20, self.second,args=[name]) timer.start() engine = pyttsx3.init() engine.say('I see '+name) engine.runAndWait() #detection def get_frame(self,flag): if flag==1: engine = pyttsx3.init() engine.say('starting object detetction live stream.') engine.runAndWait() self.video = cv2.VideoCapture(0) ret, frame = self.video.read() detected_image_array, detections = self.detector.detectObjectsFromImage(input_type="array", input_image=frame, output_type="array",minimum_percentage_probability=30) self.speak(detections) ret, jpeg = cv2.imencode('.jpg', detected_image_array) return jpeg.tobytes() def switching(self): if(self.video.isOpened()): engine = pyttsx3.init() engine.say('switching to face recognition') engine.runAndWait() self.video.release() #closing def close(self): engine = pyttsx3.init() engine.say('stopping live stream.') engine.runAndWait() self.video.release()
def ProcessImageFolderToTextFile(self, imageFolderPath, savePath, isObjDetect=False): innertexts = [] #read all images in image folder fileList = os.listdir(imageFolderPath) detector = ObjectDetection() if isObjDetect: #detect the object and obtain a list detector.setModelTypeAsRetinaNet() detector.setModelPath( os.path.join(imageFolderPath, "resnet50_coco_best_v2.0.1.h5")) detector.loadModel() i = 1 #read image one by one and save the innertext in the list for file in fileList: print("Processing ", str(i), " image: ", file) i += 1 if not (".png" in file.lower() or ".jpg" in file.lower() or ".gif" in file.lower()): print("Image type wrong:", file) else: fullfilePath = os.path.abspath( imageFolderPath.split("\\")[-2] + "\\" + file) imagebase64 = base64.b64encode(open(fullfilePath, 'rb').read()) #Read the image - inner text and object detection try: #read inner text innerTag = (self.readImageBase64Text( imagebase64, "eng").replace('\n', '').replace(' ', '').replace('|', '')) #read object detection if isObjDetect: innerTag += (self.readImageBase64Object( imagebase64, detector).replace('\n', '').replace(' ', '').replace('|', '')) except Exception as e: print("ProcessImageFolderToTextFile ex " + fullfilePath + ": " + str(e)) #if no innertext found, write no tag innerTag = ("NoTag") if innerTag == "" else (innerTag.lower()) #image in base64 formate imgType = imghdr.what(fullfilePath) imagebase64 = base64.b64encode(open(fullfilePath, 'rb').read()) #add to list innertext = file + "|" + innerTag + "|data:image/" + imgType + ";base64," + str( imagebase64, 'utf-8') innertexts.append(innertext) #write the information into the target file with open(savePath + datetime.datetime.now().strftime("%m%d%Y%H%M%S%f") + ".txt", 'a', encoding='utf-8') as the_file: the_file.writelines("Image|Tag|Base64\n") for txt in innertexts: the_file.writelines(txt.strip() + '\n')
def extractor(self, image_objects): #inizializzo la lista delle classi da chiamare per verificare le condizioni lista_classi = [] #Estraggo la lista delle differenti tipologie di condizione lista_condizioni = list(self.condition_list.keys()) if 'size' in lista_condizioni: ''' Appendo a 'lista_classi' le diverse chiamate alla classe che gestisce la condizione sulla 'size' (dimensione del file). Creo in questo modo due oggetti diversi, uno per soddisfare la dimensione minima e uno per la dimensione massima. ''' min_value = self.condition_list['size']['min'] max_value = self.condition_list['size']['max'] lista_classi.append( SizeChecker(self.condition_list, min_value, max_value)) if 'time' in lista_condizioni: #Appendo a 'lista_classi' le diverse chiamate alla classe #che gestisce la condizione sul 'time' (data di creazione del file). min_value = self.condition_list['time']['min'] max_value = self.condition_list['time']['max'] lista_classi.append( TimeChecker(self.condition_list, min_value, max_value)) if 'wordlist' in lista_condizioni: ''' Appendo a 'lista_classi' le diverse chiamate alla classe che gestisce la condizione sulla 'wordlist' (lista delle parole cercate). Creo tanti oggetti della classe 'OccurrenceChecker' quante sono le coppie parola-occorrenza cercate. ''' for parola in self.condition_list['wordlist'].keys(): lista_classi.append( OccurrenceChecker(parola, self.condition_list['wordlist'][parola])) if 'objectlist' in lista_condizioni: detector = ObjectDetection() model_path = "./models/yolo-tiny.h5" detector.setModelTypeAsTinyYOLOv3() detector.setModelPath(model_path) detector.loadModel() #Appendo a 'lista_classi' le diverse chiamate alla classe #che gestisce la condizione sulla 'objectlist' (lista degli oggetti cercati). for obj in self.condition_list['objectlist'].keys(): #controllo se l'oggetto è tra quelli riconoscibili dall'algoritmo if obj in image_objects: lista_classi.append( ImageChecker(self.condition_list, obj, self.condition_list['objectlist'][obj], detector)) return lista_classi #Restituisco la lista contenente la chiamata alle classi per ogni specifica condizione
def open_cam(selcam): cam = cv2.VideoCapture(selcam) ic = 0 while True: test, frame = cam.read() cv2.imshow("PRESS SPACE TO CAPTURE AND ESC TO CLOSE", frame) if not test: break k = cv2.waitKey(1) if k % 256 == 27: # ESC pressed print("Escape hit, closing...") messagebox.showinfo( "Processing", "No pictures = %d\n Click to process further!\nPLEASE BE PATIENT TILL THE PROCESS COMPLETES" % ic) break elif k % 256 == 32: # SPACE pressed img_name = "img\\image_%d.jpg" % ic ic = ic + 1 cv2.imwrite(img_name, frame) print("{} written!".format(img_name)) cam.release() cv2.destroyAllWindows() ################################################################ from imageai.Detection import ObjectDetection import os st = time.time() execution_path = os.getcwd() obj = {} detector = ObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath( os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5")) detector.loadModel() count = 0 for i in range(0, ic): detections, extracted = detector.detectObjectsFromImage( input_image=os.path.join(execution_path, "img\\image_%d.jpg" % i), output_image_path=os.path.join(execution_path, "img\\imagenew_%d.jpg" % i), extract_detected_objects=True) df = pd.DataFrame(detections) # df1=pd.DataFrame(extracted) df["Extracted Img"] = extracted print(df) #creating excel sheet print("img\\image_%d.jpg" % i) base = os.path.basename("img\\image_%d.jpg" % i) writer = pd.ExcelWriter(r'countexcel/%s.xlsx' % base, engine='xlsxwriter') df.to_excel(writer, sheet_name='Sheet1') writer.save() print(base) for eachObject in detections: if eachObject["name"] == "person": count = count + 1 print(eachObject["name"], " : ", eachObject["percentage_probability"]) obj[eachObject["name"]] = eachObject["percentage_probability"] print("No of person is %d" % count) en = time.time() total = en - st print(total) #print('\n'.join(obj)) # for i in range(0, ic): #os.remove(os.path.join(execution_path , r"img/image_%d.jpg" %i)) #os.remove(os.path.join(execution_path , r"img/imagenew_%d.jpg" %i)) return count, ic, obj
from imageai.Detection import ObjectDetection, VideoObjectDetection from glob import glob import os ''' TODO Read upon other models at https://github.com/OlafenwaMoses/ImageAI/releases/tag/1.0/ ''' detection_obj = ObjectDetection() detection_obj.setModelTypeAsRetinaNet( ) # you can choose between retinaNet, YOLOv3 and TinyYOLOv3 detection_obj.setModelPath("models/resnet50_coco_best_v2.0.1.h5") detection_obj.loadModel() for img in glob("inputs/*"): detections, extracted_images = detection_obj.detectObjectsFromImage( input_image=img, output_image_path=os.path.join("outputs/", f'detected_{str(img).split("/")[1]}'), extract_detected_objects=True, minimum_percentage_probability=80) # the last parameter allows us to extract images retrieved by the bounding box as independent images # adjust minimum probability to set a threshold where the output obeys. for eachObject in detections: print(eachObject["name"], " : ", eachObject["percentage_probability"] ) # retrieve the object name and relevant probability
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 control(): global num_seed num_seed = 0 model = load_model(MODEL_NAME) #----------- imagede = '/home/pi/Desktop/photos/default.jpg' img_default = load_image(imagede) classify(model, img_default) ##初始化 pygame.init() ##变量存放处 size = width, height = 300, 200 bgColor = (0, 0, 0) ##設置界面寬高 screen = pygame.display.set_mode(size) ##設置標題 pygame.display.set_caption("Team 1 Monitor") ##要在Pygame中使用文本,必须创建Font对象 ##第一个参数指定字体 ,第二个参数指定字体大小 font = pygame.font.Font(None, 20) ##调用get_linesize()方法获得每行文本的高度 line_height = font.get_linesize() position = 0 screen.fill(bgColor) ##创建一个存放的文本TXT # f = open("record.txt",'w') while True: for event in pygame.event.get(): if event.type == pygame.QUIT: # 關閉文件 # f.close() sys.exit() # print('GG\n') if event.type == pygame.KEYDOWN: # f.write(str(event) + '\n') if event.key == K_w: # print('w\n') cm.send('#W') elif event.key == K_s: cm.send('#S') if event.key == K_j: # print('w\n') cm.send('#w') elif event.key == K_k: cm.send('#s') elif event.key == K_d: cm.send('#D') elif event.key == K_a: cm.send('#A') elif event.key == K_x: cm.send('#x') elif event.key == K_b: cm.send('#b') # -------------------------------------------- elif event.key == K_p: camera.stop() imagepath = '/home/pi/Desktop/photos/' + str( num_seed) + '.jpg' img = load_image(imagepath) label, prob, _ = classify(model, img) print( 'we think image name:{} with certainty {} that it is {}' .format(imagepath, prob, label)) # ------------------------------ # 目标跟随,返回 # hd5文件请放在执行文件目录下,输入输出在photos文件夹 elif event.key == K_g: camera.stop() imagepath = '/home/pi/Desktop/photos/' + str( num_seed) + '.jpg' outputpath = '/home/pi/Desktop/photos/' + str( num_seed) + 'new.jpg' execution_path = os.getcwd() detector = ObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath( os.path.join(execution_path, 'resnet50_coco_best_v2.0.1.h5')) detector.loadModel() a = time.time() custom_objects = detector.CustomObjects(bottle=True) detections = detector.detectCustomObjectsFromImage( custom_objects=custom_objects, input_image=imagepath, output_image_path=outputpath, minimum_percentage_probability=50, box_show=True) b = time.time() print('the time is {}'.format(b - a)) print('the direction is {}'.format( detections[0]['direction'])) for eachObject in detections: print(eachObject['name'] + ':' + eachObject['percentage_probability']) elif event.key == K_t: num_seed = camera.capture(num_seed) elif event.key == K_q: camera.stop() print("==End of Photograph==") elif event.key == K_o: camera.start() print("==Begin of Photograph==") elif event.key == K_r: camera.record() # render()将文本渲染成Surface对象 # 第一个参数是带渲染的文本 # 第二个参数指定是否消除锯齿 # 第三个参数指定文本的颜色 screen.blit(font.render(str(event), True, (0, 255, 0)), (0, position)) position += line_height if position >= height: position = 0 screen.fill(bgColor) pygame.display.flip()
def main(): detector = ObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath( os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5")) detector.loadModel("faster") s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: #s.bind((socket.gethostname(), 8000)) s.bind((HOST, 8000)) except socket.error as err: print("Error de bind") sys.exit() s.listen(5) img = carga_imagen("listo.png") canvas.create_image(0, 0, image=img, anchor=NW) text.config(state="normal") text.delete(1.0, END) text.insert(INSERT, "Esperando una conexion") text.config(state="disabled") while True: print("esperando una conexion") clientsocket, address = s.accept() print(f"Conexion desde {address} establecida ") f = open("recibido.png", "wb") img = carga_imagen("cargando1.png") canvas.create_image(0, 0, image=img, anchor=NW) full_msg = '' #msg = clientsocket.recv(1024) #print(msg) #temp = msg[22:] #full_msg += temp.decode("utf-8") while True: msg = clientsocket.recv(1024) #print(msg) if len(msg) < 1024: try: #temp = msg[7:] #full_msg += temp.decode("utf-8") full_msg += msg.decode("utf-8") except: print("No se pudo decodificar el mensaje") break try: full_msg += msg.decode("utf-8") except: print("No se pudo decodificar el mensaje") #msg = clientsocket.recv(100000) print(full_msg) try: js = literal_eval(str(full_msg)) except: print("No se pudo convertir el mensaje a formato json") text.config(state="normal") text.delete(1.0, END) text.insert( INSERT, "Datos sensor ultrasonico: " + str(js["distancia"]) + "cm de distancia\n\nDatos camara: Fotografia con un peso de " + str(len(js["imagen"])) + " bytes\n\n") text.config(state="disabled") try: imagen = base64.b64decode(js["imagen"]) f.write(imagen) detections = detector.detectObjectsFromImage( input_image=os.path.join(execution_path, "recibido.png"), output_image_path=os.path.join(execution_path, "imagenew.png"), minimum_percentage_probability=40) img = carga_imagen("imagenew.png") canvas.create_image(0, 0, image=img, anchor=NW) objetos_detectados = compilar1(detections) """if len(detections) != 0: for eachObject in detections: objetos_detectados += ""+str(verificarGen(traducir(eachObject["name"])))+ ";" + str(eachObject["percentage_probability"] )+" " else: objetos_detectados += "una pared u objeto desconocido " """ text.config(state="normal") text.insert( INSERT, "Objeto(s) identificado(s): " + objetos_detectados + "\n\n") msg_to_send = compilar2(detections, js) #msg_to_send = direccion(str(js["direccion"]))+"hay "+objetos_detectados+"a "+str(js["distancia"])+"cm de distancia de usted" print("Natural: " + msg_to_send) print("\nMorse: " + str(encode(msg_to_send))) text.insert( INSERT, "Mensaje en lenguaje natural: " + msg_to_send + "\n\nMensaje en codigo morse: " + encode(msg_to_send) + "\n\n") text.config(state="disabled") clientsocket.send(bytes(msg_to_send, "utf-8")) clientsocket.close() except: print("Error grave") clientsocket.close() img = carga_imagen("listo.png") canvas.create_image(0, 0, image=img, anchor=NW) text.config(state="normal") text.delete(1.0, END) text.insert(INSERT, "Esperando una conexion") text.config(state="disabled")
def object_detection(): execution_path = os.getcwd() detector = ObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath(settings.OBJECT_RECOGNITION_MODEL) detector.loadModel() done = 0 total_photos = Photo.objects.count() t = datetime.now() for photo in Photo.objects.all().order_by('pk'): if not photo.exists(check_input=True): logger.warning("Path does not exist!", id=photo.id, name=photo.name) total_photos -= 1 continue if photo.get_objects(): total_photos -= 1 continue ext = photo.name.split('.')[1] tmp_file = os.path.join(execution_path, f"photo.{ext}") new_tmp_file = os.path.join(execution_path, f"photo_out.{ext}") if os.path.islink(tmp_file): os.unlink(tmp_file) os.symlink(photo.get_input_path(), tmp_file,) detections = detector.detectObjectsFromImage( input_image=tmp_file, output_image_path=new_tmp_file ) with transaction.atomic(): for detection in detections: object_category, _ = ObjectCategory.objects.get_or_create(name=detection["name"]) ObjectIdentification.objects.create( photo=photo, category=object_category, confidence=detection["percentage_probability"] ) os.unlink(tmp_file) if os.path.isfile(photo.get_output_path()): os.remove(photo.get_output_path()) os.rename(new_tmp_file, photo.get_output_path()) done += 1 elapsed_time = (datetime.now() - t).seconds estimated_time_left = elapsed_time / done * (total_photos - done) stats = dict( name=photo.name, id=photo.pk, done=done, left=total_photos-done, proc=f'{"%.2f" % (done / total_photos * 100)}', est_left=f'{"%.2f" % estimated_time_left}s' ) if not len(detections): logger.warning( "No objects found!", **stats ) else: logger.info( "Detecting", **stats ) logger.info("Finished image recognition")
def getImageDetails(): image = request.files print() print(image) filestr = image['img'].read() #convert string data to numpy array npimg = numpy.fromstring(filestr, numpy.uint8) # convert numpy array to image print(1) img = cv2.imdecode(npimg, cv2.IMREAD_UNCHANGED) print(1) # img = cv2.imread(filename) cv2.imshow("image from post", img) cv2.imwrite("image_from_post_request.jpeg", img) print(1) cv2.waitKey(0) cv2.destroyAllWindows() # plt.imshow(img) # plt.show() filename = "image_from_post_request.jpeg" execution_path = os.getcwd() file_name = "image_from_post_request" detector = ObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath( os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5")) detector.loadModel() detections = detector.detectObjectsFromImage( input_image=os.path.join(execution_path, file_name + ".jpeg"), output_image_path=os.path.join(execution_path, file_name + "detected.jpeg")) image = cv2.imread(file_name + ".jpeg") copy = image.copy() face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # ROI_number = 0 # return(jsonify({"out" : "Success !!!!"})) output = { "humna_face": { "count": 0 }, "animal": { "count": 0, "objects_found": dict() }, "object": { "count": 0, "objects_found": dict() } } animal = [ "bird", "cat", "dog", 'horse', "sheep", "cow", "elephant", "bear", "zebra", "giraffe" ] for eachObject in detections: print(output, eachObject.keys()) print(eachObject["name"], " : ", eachObject["percentage_probability"]) if (eachObject['name'] == 'person'): x, y, w, h = eachObject['box_points'] ROI = image[y:y + h, x:x + w] cv2.imwrite('ROI.jpeg', ROI) cv2.rectangle(copy, (x, y), (x + w, y + h), (36, 255, 12), 2) person = cv2.imread('ROI.jpeg') # Convert into grayscale gray = cv2.cvtColor(person, cv2.COLOR_BGR2GRAY) # Detect faces faces = face_cascade.detectMultiScale(gray, 1.4, 1, minSize=(1, 1)) if (len(faces) == 0): pass else: output['human_face']['count'] += 1 elif (eachObject['name'] in animal): output['animal']['count'] += 1 if (eachObject['name'] not in output['animal']['objects_found'].keys()): output['animal']['objects_found'][eachObject['name']] = 0 output['animal']['objects_found'][eachObject['name']] += 1 else: output['object']['count'] += 1 if (eachObject['name'] not in output['object']['objects_found'].keys()): output['object']['objects_found'][eachObject['name']] = 0 output['object']['objects_found'][eachObject['name']] += 1 # return(send_file(filename_or_fp = file_name + "detected.jpeg", mimetype="image/gif")) # return(m.to_string(), {'Content-Type': m.content_type}) return (jsonify(output))
def crop(sample=False): file_path = os.path.dirname(os.path.abspath(__file__)).replace('\\', '/') origin_images_path = file_path + "/../data/raw_data/cars_train_new" destination_images_path = file_path + "/../data/object_detection_data/output_images_YOLO" final_images_path = file_path + "/../data/object_detection_data" if not os.path.exists(destination_images_path): os.makedirs(destination_images_path) logging.info('Starting detecting objects') detector = ObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath(file_path + "/../data/raw_data/YOLO_weights/yolo.h5") detector.loadModel() images = os.listdir(origin_images_path) if sample: images = images[:10] for i in images: detector.detectObjectsFromImage(input_image=origin_images_path + '/' + i, output_image_path=destination_images_path + "/new_{}".format(i), extract_detected_objects=True) logging.info('Finished detecting objects') logging.info('Starting assigining objects to folder output_image_cropped') # Keep only the biggest cut car dirs = list(filter(os.path.isdir, [destination_images_path + '/' + i for i in os.listdir(destination_images_path)])) for directory in dirs: files = os.listdir(directory) cars_size = {} for file in files: if not (file.startswith('car') or file.startswith('truck')): os.unlink(directory + "/" + file) remaining_files = os.listdir(directory) for file in remaining_files: cars_size[file] = Image.open(directory + str("/") + file).size if len(cars_size) > 1: biggest_car = None dim = (0, 0) for car in cars_size.keys(): if cars_size[car][0] * cars_size[car][1] > dim[0] * dim[1]: biggest_car = car dim = cars_size[car] to_delete = (list(set(cars_size.keys()))) to_delete.remove(biggest_car) for small_car in to_delete: os.unlink(directory + str("/") + small_car) # Rename the images as number.jpg dirs = filter(os.path.isdir, [destination_images_path + '/' + i for i in os.listdir(destination_images_path)]) for directory in dirs: files = os.listdir(directory) for file in files: number = re.search(r"[0-9]+", str(directory)) new_name = str(number.group()) + ".jpg" start = directory + str("/") + file end = directory + str("/") + new_name os.rename(start, end) # Put the all the cut cars into a folder named "output_images_cropped" dirs = filter(os.path.isdir, [destination_images_path + '/' + i for i in os.listdir(destination_images_path)]) if not os.path.exists(final_images_path + "/output_images_cropped"): os.mkdir(final_images_path + "/output_images_cropped") for directory in dirs: files = os.listdir(directory) for file in files: start = directory + str("/") + file destination = str(final_images_path + "/output_images_cropped") + str("/") + file shutil.copyfile(start, destination) logging.info('Finished entire process')
import json import os import statistics as s import time from os import listdir import cv2 from scipy.spatial import distance from imageai.Detection import ObjectDetection execution_path = os.getcwd() detectorY = ObjectDetection() detectorY.setModelTypeAsYOLOv3() detectorY.setModelPath( "/Users/stuartrobinson/repos/computervision/ImageAI/gitignore/yolo.h5") detectorY.loadModel() def translateCropCoordToOrig(xy, cropCoords): return [xy[0] + cropCoords[0], xy[1] + cropCoords[1]] def distanceBetween(box_points, ballXY): print("in distanceBetween, ", box_points, ", ", ballXY) boxX = s.mean([box_points[0], box_points[2]]) boxY = s.mean([box_points[1], box_points[3]]) return distance.euclidean((boxX, boxY), (ballXY[0], ballXY[1]))
def __init__(self): # Init node image_view_detect rospy.init_node("img_detect_following") os.system("clear") execution_path = os.getcwd() detector = ObjectDetection() detector.setModelTypeAsYOLOv3() custom_objects = detector.CustomObjects(person=True) detector.setModelPath(os.path.join(execution_path, "yolo.h5")) detector.loadModel(detection_speed="fastest") # publish into cmd vel self.pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1) self.sub = rospy.Subscriber('/raspicam_node/image/compressed', CompressedImage, self.callback) self.vel = Twist() self.rate = rospy.Rate(10) self.img_root_size = [640, 480] self.frame = 1 self.input_image_path = "./webCamImage.jpg" self.output_image_path = "./outputImage.jpg" self.min_prob = 20 # time sleep ps self.rate_sleep = rospy.Rate(10) self.cv_bridge = cv_bridge.CvBridge() while not rospy.is_shutdown(): # check, frame = vs.read() # img = msg.data # np_arr = np.fromstring(img, np.uint8) # img_np = cv2.imdecode(np_arr, 1) # cv2.imwrite(input_image_path, frame) cv2.imwrite(self.input_image_path, self.frame) detections = detector.detectCustomObjectsFromImage(custom_objects=custom_objects, input_image=os.path.join(execution_path, self.input_image_path), output_image_path=os.path.join(execution_path, self.output_image_path), minimum_percentage_probability=self.min_prob) print("DA", detections) if len(detections) != 0: x1 = detections[0]["box_points"][0] y1 = detections[0]["box_points"][1] x2 = detections[0]["box_points"][2] y2 = detections[0]["box_points"][3] # print("(", x1, ", ", y1, ")") # print("(", x2, ", ", y2, ")") image_child = [x1, y1, x2, y2] self.listenAndMove(self.img_root_size, x1,y1,x2,y2) print("--------------------------------") else: print("Not found any person") self.stop() print("--------------------------------") img = cv2.imread(self.output_image_path) cv2.imshow('frame', img) # self.rate_sleep.sleep() key = cv2.waitKey(1) if key == ord("q"): self.stop() break self.rate.sleep() self.stop()
# execution_path = os.getcwd() # gets the path of where the python file and model file are at # DETECTION TO OUTPUT THE PREDICTION IN CONSOLE prediction = CustomImagePrediction() prediction.setModelTypeAsResNet( ) # sets the model type of prediction as ResNet - one we used prediction.setModelPath("idenprof_061-0.7933.h5" ) # sets model path of prediction to the ai model file prediction.setJsonPath( "idenprof_model_class.json") # sets the path of json file prediction.loadModel(num_objects=10) # DETECTION TO CREATE NEW IMAGES ON FOUND ITEMS detector = ObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath("resnet50_coco_best_v2.0.1.h5") detector.loadModel() detections, path = detector.detectObjectsFromImage( input_image="image.jpg", # takes in the input image output_image_path="newimage.jpg", # creates a new image # creates new images from the each object extract_detected_objects=True) # DETECTION TO DRAW BOXES AROUND IN A NEW IMAGE lidl_detector = ObjectDetection() lidl_detector.setModelTypeAsRetinaNet() lidl_detector.setModelPath("resnet50_coco_best_v2.0.1.h5") lidl_detector.loadModel() lidl_detections = lidl_detector.detectObjectsFromImage(
def Make_Features(df): multiple_prediction = ImagePrediction() multiple_prediction.setModelTypeAsDenseNet() multiple_prediction.setModelPath( "../mode/trained_models/DenseNet-BC-121-32.h5") multiple_prediction.loadModel() detector = ObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath("../mode/trained_models/yolo.h5") detector.loadModel() df['DenseNet'] = df['img'].swifter.apply( lambda x: multiple_prediction.predictImage(path_dir + x, result_count=3)) df['Yolo'] = df['img'].swifter.apply( lambda x: detector.detectObjectsFromImage( path_dir + x, output_image_path='./new.jpg', minimum_percentage_probability=20)) df['DenseNet_obj'] = df['DenseNet'].swifter.apply( lambda x: x[0][0] + ' and ' + x[0][1] + ' and ' + x[0][2]) df['Yolo_obj'] = df['Yolo'].swifter.apply( lambda x: ' '.join(word for word in [l['name'] for l in x]), axis=1) df['Img_txt'] = df['DenseNet_obj'] + ' ' + df['Yolo_obj'] df['palette_color'] = df['img'].swifter.apply( lambda x: ColorThief(path_dir + x).get_palette(color_count=5)) df['Bluriness'] = [ cv2.Laplacian(cv2.imread(path_dir + x, 0), cv2.CV_64F).var() for x in df['img'] ] df['Imagedim'] = [ cv2.imread(path_dir + x).flatten().shape[0] for x in df['img'] ] df['Yolo_unique'] = df['Yolo_obj'].swifter.apply(lambda x: len(set(x))) df['Yolo_N_obj'] = df['Yolo_obj'].swifter.apply(lambda x: len(x)) #print(df.describe() ) # First cross variable between text and image : df['sim_txt_img_gen'] = df.swifter.apply( lambda x: nlp(x.text).similarity(nlp(x.DenseNet_obj)), axis=1) df['sim_txt_img_objs'] = df.swifter.apply(lambda x: nlp(x.text).similarity( nlp(' and '.join(word[0] for word in x.Yolo_obj))), axis=1) # extract dominant colors from image df['paletCol_1'] = df['palette_color'].swifter.apply( lambda x: (x[0][0] * 65536 + x[0][1] * 256 + x[0][2])) df['paletCol_2'] = df['palette_color'].swifter.apply( lambda x: (x[1][0] * 65536 + x[1][1] * 256 + x[1][2])) df['paletCol_3'] = df['palette_color'].swifter.apply( lambda x: (x[2][0] * 65536 + x[2][1] * 256 + x[2][2])) df['paletCol_4'] = df['palette_color'].swifter.apply( lambda x: (x[3][0] * 65536 + x[3][1] * 256 + x[3][2])) df['paletCol_5'] = df['palette_color'].swifter.apply( lambda x: (x[4][0] * 65536 + x[4][1] * 256 + x[4][2])) # Get Blurry status # Get shapes df['brightness'] = [ cv2.mean(cv2.cvtColor(cv2.imread(path_dir + x), cv2.COLOR_BGR2HSV))[1] / 255. for x in df['img'] ] df['Saturation'] = [ cv2.mean(cv2.cvtColor(cv2.imread(path_dir + x), cv2.COLOR_BGR2HSV))[0] / 255. for x in df['img'] ] df['ImageValue'] = [ cv2.mean(cv2.cvtColor(cv2.imread(path_dir + x), cv2.COLOR_BGR2HSV))[2] / 255. for x in df['img'] ] df['word_count'] = df['text'].swifter.apply( lambda x: len(str(x).split(" "))) df['char_count'] = df['text'].str.len() df['stp_count'] = df['text'].swifter.apply( lambda x: len([x for x in x.split() if x in stop])) df['spc_count'] = df['text'].swifter.apply( lambda x: len([x for x in list(x) if x in special_char])) df['sentiment_txt'] = df['text'].swifter.apply(lambda x: getsentiment(x)) df['sentiment_img'] = df['DenseNet_obj'].swifter.apply( lambda x: getsentiment(x)) df['prfn_ftr'] = df['text'].swifter.apply(lambda x: nlp(x)._.is_profane) df['Quant'] = df['text'].swifter.apply(lambda x: len([ y for y in nlp(x).ents if str(y.label_) == 'MONEY' or str(y.label_) == 'DATE' or str(y.label_) == 'TIME' or str(y.label_) == 'PERCENT' or str(y.label_) == 'ORDINAL' or str(y.label_) == 'CARDINAL' or str(y.label_) == 'QUANTITY' ])) df['Ent'] = df['text'].swifter.apply(lambda x: len([ y for y in nlp(x).ents if str(y.label_) == 'PERSON' or str(y.label_) == 'NORP' or str( y.label_) == 'ORG' or str(y.label_) == 'LOC' or str(y.label_) == 'GPE' or str(y.label_) == 'WORK_OF_ART' or str(y.label_) == 'EVENT' ])) df['polarity_scores'] = df['text'].swifter.apply( lambda x: sid.polarity_scores(x)) df['neg_txt'] = df['polarity_scores'].swifter.apply(lambda x: x['neg']) df['neu_txt'] = df['polarity_scores'].swifter.apply(lambda x: x['neu']) df['pos_txt'] = df['polarity_scores'].swifter.apply(lambda x: x['pos']) df['com_txt'] = df['polarity_scores'].swifter.apply( lambda x: x['compound']) #df = df.drop(columns=['DenseNet' ,'DenseNet_obj', 'Yolo' , 'Yolo_obj' , 'palette_color', 'polarity_scores']) return df
def __init__(self, yolo_model_path): self.yolo_model_path = yolo_model_path self.object_detector = ObjectDetection() self.object_detector.setModelTypeAsYOLOv3() self.object_detector.setModelPath(yolo_model_path) self.object_detector.loadModel()
import cv2 import numpy as np from controller import saveImage, randomName import json import os import requests app = Flask(__name__) CORS(app, resources={ r"/*": { "origins" : "*" } }) # Inicia a Lib de Detecção de Imagens DETECTOR = ObjectDetection() DETECTOR.setModelTypeAsYOLOv3() DETECTOR.setModelPath( "../yolo.h5" ) DETECTOR.loadModel(detection_speed="flash") @app.route( '/v1/detection', methods = ['POST'] ) def v1_detection(): url_image = request.json.get('url') if not url_image: json_resp = { 'ok' : False, 'msg' : 'Não encontrado url', 'data' : None
# coding: utf-8 from imageai.Detection import ObjectDetection import os execution_path = os.getcwd() # imageAI根目录 detector = ObjectDetection() # 加载检测器 #resnet png格式可以正常输出文本,且有标记的新图片 detector.setModelTypeAsRetinaNet() # 设置模型网络类型 detector.setModelPath( os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5")) # 设置模型文件路径 # YOLOv3 用不了 # detector.setModelTypeAsYOLOv3() # detector.setModelPath( # os.path.join(execution_path, 'models', 'Object Detection', 'yolo.h5')) # TinyYOLOv3 png格式可以正常输出文本,且有标记的新图片 # ============================================================================= # detector.setModelTypeAsTinyYOLOv3() # detector.setModelPath( # os.path.join(execution_path, 'models', 'Object Detection', # "yolo-tiny.h5")) # ============================================================================= detector.loadModel() # 加载模型 detections = detector.detectObjectsFromImage( input_image=os.path.join(execution_path, "image2.jpg"), # 输入待检测图片路劲 output_image_path=os.path.join(execution_path, "image2new.png"), # 输出图片路径 特别提醒用png别用jpg
import matplotlib matplotlib.use('agg') import matplotlib.pyplot as plt from imageai.Detection import ObjectDetection import os workdir = os.getcwd() find_objects = ObjectDetection() find_objects.setModelTypeAsRetinaNet() # Use a pre-trained model. # Don't reinvent the wheel. Either use it, or improve it. find_objects.setModelPath(os.path.join(workdir, "resnet50_coco_best_v2.1.0.h5")) find_objects.loadModel() objects_found = find_objects.detectObjectsFromImage( input_image=os.path.join(workdir, "ilia.jpg"), output_image_path=os.path.join(workdir, "iliaRevealed.jpg")) for objects in objects_found: print(objects["name"] + " : " + str(objects["percentage_probability"]))
#inspired by https://stackabuse.com/object-detection-with-imageai-in-python/ #sample image gotten from https://pixabay.com/photos/traffic-jam-traffic-india-street-388924/ from imageai.Detection import ObjectDetection detectorObj = ObjectDetection() model_h5_path = "./models/yolo-tiny.h5" input_file_path = "./input/traffic-jam-388924_640.jpg" output_file_path = "./output/output.jpg" detectorObj.setModelTypeAsTinyYOLOv3() detectorObj.setModelPath(model_h5_path) detectorObj.loadModel() detectionResult = detectorObj.detectObjectsFromImage( input_image=input_file_path, output_image_path=output_file_path) for DetectedItem in detectionResult: print(DetectedItem["name"], " : ", DetectedItem["percentage_probability"])
# https://towardsdatascience.com/object-detection-with-10-lines-of-code-d6cb4d86f606 import os from paths import ROOT_DIR from imageai.Detection import ObjectDetection # Download this : https://github.com/OlafenwaMoses/ImageAI/releases/download/essentials-v5/resnet50_coco_best_v2.1.0.h5/ humanDetector = ObjectDetection() humanDetector.setModelTypeAsRetinaNet() humanDetector.setModelPath( os.path.join(ROOT_DIR, "Models/resnet50_coco_best_v2.1.0.h5")) humanDetector.loadModel() def objectDetect(image_path): file_head = os.path.split(image_path)[0] file_tail = os.path.splitext(os.path.split(image_path)[1])[0] face_path_head = os.path.join(file_head, file_tail + '_detected') detections, extracted_images = humanDetector.detectObjectsFromImage( image_path, output_image_path=os.path.join(face_path_head, "Detected_" + image_path), extract_detected_objects=True) for Object in detections: print(Object["name"], " : ", Object["percentage_probability"]) def personDetect(image_path): file_head = os.path.split(image_path)[0]
HEIGHT = 480 D_F = (0.5 * WIDTH) / math.tan(0.5 * D_FOV_X) V_F = (0.5 * WIDTH) / math.tan(0.5 * V_FOV_X) resized = False def get_depth(): return frame_convert2.pretty_depth_cv( freenect.sync_get_depth(format=freenect.DEPTH_REGISTERED)[0]) def get_video(): return frame_convert2.video_cv(freenect.sync_get_video()[0]) video_detector = ObjectDetection() video_detector.setModelTypeAsYOLOv3() video_detector.setModelPath(os.path.join(execution_path, "yolo.h5")) objects = video_detector.CustomObjects(person=True) video_detector.loadModel() plt.show() cv2.namedWindow('Depth') cv2.namedWindow('Video') #gui = pygame_gui.LocationsGUI() #gui.start() while 1: video = get_video() depth = freenect.sync_get_depth(format=freenect.DEPTH_REGISTERED)[0] returned_image, detections = video_detector.detectCustomObjectsFromImage( custom_objects=objects,
import os import time from imageai.Detection import ObjectDetection execution_path = os.getcwd() detectorY = ObjectDetection() detectorYT = ObjectDetection() detectorR = ObjectDetection() detectorY.setModelTypeAsYOLOv3() detectorYT.setModelTypeAsTinyYOLOv3() detectorR.setModelTypeAsRetinaNet() detectorR.setModelPath( "/Users/stuartrobinson/repos/computervision/ImageAI/gitignore/resnet50_coco_best_v2.0.1.h5" ) detectorYT.setModelPath( "/Users/stuartrobinson/repos/computervision/ImageAI/gitignore/yolo-tiny.h5" ) detectorY.setModelPath( "/Users/stuartrobinson/repos/computervision/ImageAI/gitignore/yolo.h5") detectorY.loadModel() detectorYT.loadModel() detectorR.loadModel() # inputImage = os.path.join(execution_path, "images", "image3.jpg") # inputImage = "/Users/stuartrobinson/repos/computervision/andre_aigassi/images/tennis_video/frames/raw/backhand/000001.png"
from imageai.Detection import ObjectDetection import cv2 """Create instance of Object Detection class""" det = ObjectDetection() """Setting paths for input image, output image and pretrained model weights of tiny yolo""" model_path = "yolo-tiny.h5" input_path = "input.jpg" output_path = "prediction_output.jpg" """Setting the model to tiny yolov3 and loading the weights from the specified path""" det.setModelTypeAsTinyYOLOv3() det.setModelPath(model_path) det.loadModel() """Detecting objects from the image and displaying the label if the prediction has minimum 0.1 probability.""" detection = det.detectObjectsFromImage(input_image=input_path, output_image_path=output_path, minimum_percentage_probability=0.1) """Result: Input and output image""" i1 = cv2.imread("input.jpg") cv2.imshow(i1) i2 = cv2.imread("prediction_output.jpg") cv2.imshow(i2) """Total objects detected""" print("Enter object name to get the box location") print("Available options are:") count = 1
from imageai.Detection import ObjectDetection import cv2 import requests import numpy as np import time detector =ObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath("resnet50_coco_best_v2.0.1.h5") detector.loadModel(detection_speed = "normal" ) url = "" ti=0 t=0 tim=0 while t<10: #ret, img = cap.read() img_resp = requests.get(url) img_arr = np.array(bytearray(img_resp.content) , dtype = np.uint8) img = cv2.imdecode(img_arr , -1) start = time.clock() custom = detector.CustomObjects(person=True)
def initialize(self, cam_number): self.detector = ObjectDetection() self.detector.setModelTypeAsTinyYOLOv3() self.detector.setModelPath(self.model_path) self.detector.loadModel()