def window_closed(self, is_windows, is_conda, key): """ Check whether the window has been closed MS Windows doesn't appear to read the window state property properly, so we check for a negative key press. Conda (tested on Windows) doesn't appear to read the window state property or negative key press properly, so we arbitrarily use another property """ # pylint: disable=no-member logger.trace("Commencing closed window check") closed = False prop_autosize = cv2.getWindowProperty('Frame', cv2.WND_PROP_AUTOSIZE) prop_visible = cv2.getWindowProperty('Frame', cv2.WND_PROP_VISIBLE) if self.arguments.disable_monitor: closed = False elif is_conda and prop_autosize < 1: closed = True elif is_windows and not is_conda and key == -1: closed = True elif not is_windows and not is_conda and prop_visible < 1: closed = True logger.trace("Completed closed window check. Closed is %s", closed) if closed: logger.debug("Window closed detected") return closed
def run(self): while cv.getWindowProperty('img', 0) != -1 or cv.getWindowProperty('watershed', 0) != -1: ch = cv.waitKey(50) if ch == 27: break if ch >= ord('1') and ch <= ord('7'): self.cur_marker = ch - ord('0') print('marker: ', self.cur_marker) if ch == ord(' ') or (self.sketch.dirty and self.auto_update): self.watershed() self.sketch.dirty = False if ch in [ord('a'), ord('A')]: self.auto_update = not self.auto_update print('auto_update if', ['off', 'on'][self.auto_update]) if ch in [ord('r'), ord('R')]: self.markers[:] = 0 self.markers_vis[:] = self.img self.sketch.show() cv.destroyAllWindows()
def draw(self): """draw new image whenever one arrives """ if self.last_image is None: return #Hack to check if the window was closed: try: cv2.getWindowProperty(self.name, 0) except: self.destroy() return # The arriving image does not support color, so we convert it img = cv2.cvtColor(self.last_image, cv2.COLOR_GRAY2RGB) # The arriving image is pretty small, we change that here mat = cv2.resize(img, (0, 0), fx=self.FACTOR, fy=self.FACTOR) # Now we draw the debug-shapes if self.last_shapes is not None: draw_debug_shapes(mat, self.last_shapes) # When that is done all that is left is show the image cv2.imshow(self.name, mat)
def draw(self): """draw new image whenever one arrives """ print "Drawing Image!" if self.last_image is None: return #Hack to check if the window was closed: try: cv2.getWindowProperty(self.name, 0) except Exception as e: print "Destroying DebugImageView: %s" % e self.destroy() return # The arriving image does not support color, so we convert it img = cv2.cvtColor(self.last_image, cv2.cv.CV_GRAY2RGB) # The arriving image is pretty small, we change that here cv2.resize(img, (self.FACTOR, self.FACTOR)) # Now we draw the debug-shapes if self.last_shapes is not None: draw_debug_shapes(img, self.last_shapes) # When that is done all that is left is show the image cv2.imshow(self.name, img)
def __init__(self, files): done = False for image in itertools.cycle(files): cv2.imshow('image', cv2.cvtColor(numpy.array(image), cv2.COLOR_RGB2BGR)) while True: key = cv2.waitKey(1) & 0xFF if key == ord("f") or key == ord('n'): break if key == ord("q") or cv2.getWindowProperty('image', 0) < 0: cv2.destroyAllWindows() done = True break if done: break
def __init__(self, folder): images = [f for f in listdir(folder) if isfile(join(folder, f))] cv2.namedWindow('image', cv2.WINDOW_NORMAL) done = False for image in itertools.cycle(images): img = cv2.imread(folder + "/" + image) cv2.imshow('image', img) while True: key = cv2.waitKey(1) & 0xFF if key == ord("f") or key == ord('n'): break if key == ord("q") or cv2.getWindowProperty('image', 0) < 0: cv2.destroyAllWindows() done = True break if done: break
def run_images(valid_output, validated_image_filename, graph, input_image_filename_list): cv2.namedWindow(CV_WINDOW_NAME) for input_image_file in input_image_filename_list : # read one of the images to run an inference on from the disk infer_image = cv2.imread(input_image_file) # run a single inference on the image and overwrite the # boxes and labels test_output = run_inference(infer_image, graph) # Test the inference results of this image with the results # from the known valid face. matching = False if (face_match(valid_output, test_output)): matching = True text_color = (0, 255, 0) match_text = "MATCH" print('PASS! File ' + input_image_file + ' matches ' + validated_image_filename) else: matching = False match_text = "NOT A MATCH" text_color = (0, 0, 255) print('FAIL! File ' + input_image_file + ' does not match ' + validated_image_filename) overlay_on_image(infer_image, input_image_file, matching) cv2.putText(infer_image, match_text + " - Hit key for next.", (30, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.5, text_color, 1) # check if the window is visible, this means the user hasn't closed # the window via the X button prop_val = cv2.getWindowProperty(CV_WINDOW_NAME, cv2.WND_PROP_ASPECT_RATIO) if (prop_val < 0.0): print('window closed') break # display the results and wait for user to hit a key cv2.imshow(CV_WINDOW_NAME, infer_image) cv2.waitKey(0)
clone = image.copy() cv2.namedWindow("image") cv2.setMouseCallback("image", click_and_crop) while True: # display the image and wait for a keypress cv2.imshow("image", image) key = cv2.waitKey(20) #& 0xFF # if the 'r' key is pressed, reset the cropping region if key == ord("r"): image = clone.copy() # if the 'c' key is pressed, break from the loop elif key == ord("q"): print("Exit") cv2.destroyAllWindows break if cv2.getWindowProperty('image', cv2.WND_PROP_VISIBLE) < 1: break # if there are two reference points, then crop the region of interest # from the image and display it if len(refPt) == 2: # display soa soa = draw_soa_map(default_loader(args.image), net, refPt) cv2.imshow("Second order attention", soa) cv2.waitKey(20) # close all open windows cv2.destroyAllWindows()
def play_video(self): shown_for_first_time = False while True: if shown_for_first_time and cv2.getWindowProperty( self.WINDOW_NAME, cv2.WND_PROP_VISIBLE) <= 0: # Window closed. Abort raise ManualTaggingExitedException( "Tagging operation aborted by closing window") frame = self.vcm.next().copy() frame_index = self.vcm.get_frame_index() frame = self.modify_frame(frame, frame_index) frame = self.get_mode_handler().modify_frame(frame, frame_index) cv2.imshow(self.WINDOW_NAME, self.build_frame(frame)) shown_for_first_time = True cv2.setTrackbarPos(self.PROGRESS_BAR_NAME, self.WINDOW_NAME, frame_index) self.key_mapper.append(cv2.waitKey(self.frame_rate) & 0xFF) if self.key_mapper.consume(("esc", "esc")): # Escape key res = ButtonPopup( "Confirm restart", "Hitting confirm will destroy all progress. You will have to restart. Continue?", ["Confirm", "Cancel"], ).run() if res == "Confirm": raise ManualTaggingAbortedException( "Tagging operation aborted") elif self.key_mapper.consume(("enter", "enter")): # Enter if not self.can_commit(): self.logger.log("[ERROR] Commit operation failed") else: res = ButtonPopup("Confirm commit", self.get_commit_message(), ["Confirm", "Cancel"]).run() if res == "Confirm": break elif self.key_mapper.consume("h"): window = HelpPopup( "GUI controls reference", self.INSTRUCTIONS + [["", ""]] + self.get_mode_handler().INSTRUCTIONS, ) window.run() elif self.key_mapper.consume("a"): self.vcm.shift_frame_index(-1) elif self.key_mapper.consume("s"): self.vcm.shift_frame_index(-10) elif self.key_mapper.consume("d"): self.vcm.shift_frame_index(1) elif self.key_mapper.consume("w"): self.vcm.shift_frame_index(10) elif self.key_mapper.consume(" "): cv2.setTrackbarPos(self.PAUSE_BUTTON_NAME, self.WINDOW_NAME, 0 if self.vcm.get_paused() else 1) if self.key_mapper.consume("m") or self.key_mapper.consume("tab"): self.mode_handler_i += 1 self.mode_handler_i %= len(self.mode_handlers) self.logger.log("Changed mode") else: self.get_mode_handler().handle_keyboard(self.key_mapper)
if file_exists: object_list = remove_already_tracked_objects( object_list, img_path, json_file_data) if len(object_list) > 0: # get list of frames following this image next_frame_path_list = get_next_frame_path_list( video_name, img_path) # initial frame init_frame = img.copy() label_tracker = LabelTracker( 'KCF', init_frame, next_frame_path_list) # TODO: replace 'KCF' by 'CSRT' for obj in object_list: class_index = obj[0] color = class_rgb[class_index].tolist() label_tracker.start_tracker(json_file_data, json_file_path, img_path, obj, color, annotation_formats) # quit key listener elif pressed_key == ord('q'): break ''' Key Listeners END ''' if WITH_QT: # if window gets closed then quit if cv2.getWindowProperty(WINDOW_NAME, cv2.WND_PROP_VISIBLE) < 1: break cv2.destroyAllWindows()
def main(): global v_immagine global v_exit global v_click_foto global v_x global v_y global v_oracle_problems global v_win_size global v_screen_altezza global v_screen_larghezza # variabile globale che indica se si deve uscire dal programma # (in realtà è possibile uscire dal programma anche tramite il tasto q) v_exit = False # variabile globale che indica se è stata scattata la foto v_click_foto = False # variabile che riporta la posizione x per il disegno dei pittogrammi v_x = 1 # altezza e larghezza dello schermo v_screen_altezza = GetSystemMetrics(1) v_screen_larghezza = GetSystemMetrics(0) def controllo_eventi_del_mouse(event, x, y, flags, param): """ Controllo eventi del mouse (da notare le variabili globali) """ global v_immagine global v_exit global v_click_foto global v_x global v_y global v_oracle_problems global v_win_size # è stato premuto il pulsante del mouse in rilascio..controllo dove... if event == cv2.EVENT_LBUTTONUP: # è stato premuto il tasto per scattare una foto if x > (int(v_win_size[0]/2)-25) and x < (int(v_win_size[0]/2)+25) and y > (v_win_size[1]-75) and y < (v_win_size[1]-25): v_click_foto = True # è stato premuto il tasto No--> quindi vuol dire che la lettura non va considerata # e il programma ritorna a mettersi in scansione if x > v_x and x < (v_x + 70) and y > v_y and y < (v_y + 70): v_click_foto = False # è stato premuto il tasto Si--> quindi vuol dire che la lettura va considerata # valida e si deve uscire dal programma if x > v_x and x < (v_x + 70) and y > (v_y +80) and y < (v_y + 150): # Scrive il dato trovato nel DB di SMILE cv2.imwrite('foto_scattata.jpg', v_immagine) if scrivi_in_ut_kfoto()=='ko': v_oracle_problems = True # se il dato è stato scritto --> passo alla prossima lettura else: v_click_foto = False """ Ciclo principale di apertura e lettura del flusso dati proveniente dalla webcam """ # Definizione di un font per la scrittura a video e relativo colore testo font = cv2.FONT_HERSHEY_SIMPLEX v_text_color = (0,0,0) # Attiva le webcam per la cattura del video capture = cv2.VideoCapture(0) # Se la webcam non è presente o non è riconosciuta... if not capture.isOpened(): v_webcam = False #img = np.zeros((512,512,3), np.uint8) questa istruzione crea un'immagine nera # Carico un'immagine di sfondo che riporta attenzione sul fatto che la webcam non è stata trovata img = cv2.imread('KFoto.png') # Ottengo la tupla con la dimensione dell'immagine v_win_size = img.shape else: v_webcam = True # Inizia il ciclo di lettura del flusso video proveniente dalla webcam o dall'immagine fissa v_1a_volta = True v_oracle_problems = False v_pos_text_riga1 = (10, 10) v_pos_text_riga2 = (10, 20) while True: # Se premuto il tasto q oppure richiesto da apposito flag, esco dal form if (cv2.waitKey(1) & 0xFF in (ord('q'),ord('Q'))) or (v_exit): break # Se premuta la X sulla window, esco dal form if cv2.getWindowProperty('KFoto 1.0b', 0) < 0 and not v_1a_volta: break # Se webcam è presente, leggo il prossimo frame che mi sta passando if v_webcam: if not v_click_foto: ret, img = capture.read() v_immagine = img.copy() v_win_size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))) # Posizione del testo dei messaggi in fondo alla window v_pos_text_riga1 = (10, v_win_size[1]-50) v_pos_text_riga2 = (10, v_win_size[1]-20) # definisco il rettagolo che contiene il mirino e quindi il crop dell'immagine che viene effettivamente scansionata v_larghezza_mirino = 300 v_altezza_mirino = 200 v_mirino_top_left = ( int((v_win_size[0]-v_larghezza_mirino)/2), int((v_win_size[1]-v_altezza_mirino)/2) ) # Disegna il mirino (rosso se non ha letto nulla) if v_click_foto: v_colore = (0,255,0) # Disegno i pittogrammi-pulsanti v_x = int(v_win_size[0]-75) v_y = int((v_win_size[1]-150)/2) cv2.rectangle(img, (v_x,v_y), (v_x+70,v_y+70) , (0,0,255),2) cv2.rectangle(img, (v_x,v_y+80), (v_x+70,v_y+150) ,(0,255,0),2) cv2.putText(img,'No', (v_x + 17, v_y+43), font, 1, (0,0,255), 2,cv2.LINE_AA) cv2.putText(img,'Si', (v_x + 17, v_y+123), font, 1, (0,255,0), 2,cv2.LINE_AA) else: v_colore = (0,0,255) cv2.circle(img,(int(v_win_size[0]/2), v_win_size[1]-50), 40, v_colore, 2, 2) cv2.circle(img,(int(v_win_size[0]/2), v_win_size[1]-50), 30, v_colore, -2, 2) cv2.line(img, ( v_mirino_top_left[0] + int(v_larghezza_mirino/2), v_mirino_top_left[1] + int(v_altezza_mirino/2) - 20) , ( v_mirino_top_left[0] + int(v_larghezza_mirino/2), v_mirino_top_left[1] + int(v_altezza_mirino/2) + 20) , v_colore, 2) cv2.line(img, ( v_mirino_top_left[0] + int(v_larghezza_mirino/2) - 20, v_mirino_top_left[1] + int(v_altezza_mirino/2) ) , ( v_mirino_top_left[0] + int(v_larghezza_mirino/2) + 20, v_mirino_top_left[1] + int(v_altezza_mirino/2) ) , v_colore, 2) # Emette la scritta se ci sono problemi di connessione a Oracle if v_oracle_problems: cv2.putText(img,'Problema connessione a SMILE!', v_pos_text_riga1, font, 1, (0,0,255), 1, cv2.LINE_AA) # Aggiorna lo schermo cv2.imshow('KFoto 1.0b', img) # Riposiziono la finestra a destra dello schermo cv2.moveWindow('KFoto 1.0b', v_screen_larghezza - v_win_size[0], int( (v_screen_altezza - v_win_size[1]) / 2)) # Ricerco il puntatore della finestra v_win_handle = win32gui.FindWindow(None, 'KFoto 1.0b') # Se finestra trovata --> ne forzo la visualizzazione davanti a tutte le window aperte in questo momento # in questo modo l'applicazione rimane sempre in primo piano if v_win_handle is not None: win32gui.SetWindowPos(v_win_handle, win32con.HWND_TOPMOST, 0, 0, 0, 0,win32con.SWP_NOSIZE | win32con.SWP_NOMOVE) # associo la funzione che controlla gli eventi del mouse cv2.setMouseCallback('KFoto 1.0b', controllo_eventi_del_mouse) v_1a_volta = False # Chiudo la webcam e la window capture.release() cv2.destroyAllWindows()
def openfichier(ti,datacross,path_img,thrprobaUIP,patch_list_cross_slice,tabroi, cnnweigh,tabscanLung,viewstyle,slnroi): global quitl,dimtabx,dimtaby,patchi,ix,iy print 'openfichier start' quitl=False num_class=len(classif) slnt=datacross[0] dimtabx=datacross[1] dimtaby=datacross[2] print slnroi patchi=False ix=0 iy=0 (top,tail)=os.path.split(path_img) volumeroilocal={} pdirk = os.path.join(path_img,source_name) pdirkroicross = os.path.join(path_img,sroi) pdirkroifront = os.path.join(path_img,sroi3d) if ti =="cross view" or ti =="merge view" or ti =='front projected view': if os.path.exists(pdirkroicross): pdirk = pdirkroicross path_data_write=os.path.join(path_img,path_data) path_data_writefile=os.path.join(path_data_write,'volumerois') if os.path.exists(path_data_writefile): volumeroilocal=pickle.load(open(path_data_writefile, "rb" )) # print volumeroilocal sn='' else: sn=scan_bmp else: if os.path.exists(pdirkroifront): pdirk = pdirkroifront sn='' else: sn=transbmp pdirk = os.path.join(pdirk,sn) list_image={} cdelimter='_' extensionimage='.'+typei1 limage=[name for name in os.listdir(pdirk) if name.find('.'+typei1,1)>0 ] lenlimage=len(slnroi) # print len(limage), sln for iimage in limage: sln=rsliceNum(iimage,cdelimter,extensionimage) list_image[sln]=iimage # print sln image0=os.path.join(pdirk,list_image[slnroi[0]]) img = cv2.imread(image0,1) img=cv2.resize(img,(dimtaby,dimtabx),interpolation=cv2.INTER_LINEAR) cv2.namedWindow('imagepredict',cv2.WINDOW_NORMAL) cv2.namedWindow("Sliderfi",cv2.WINDOW_NORMAL) cv2.namedWindow("datavisu",cv2.WINDOW_AUTOSIZE) cv2.createTrackbar( 'Brightness','Sliderfi',0,100,nothing) cv2.createTrackbar( 'Contrast','Sliderfi',50,100,nothing) cv2.createTrackbar( 'Threshold','Sliderfi',int(thrprobaUIP*100),100,nothing) cv2.createTrackbar( 'Flip','Sliderfi',0,lenlimage-1,nothings) cv2.createTrackbar( 'All','Sliderfi',1,1,nothings) cv2.createTrackbar( 'None','Sliderfi',0,1,nothings) viewasked={} for key1 in usedclassif: # print key1 viewasked[key1]=True cv2.createTrackbar( key1,'Sliderfi',0,1,nothings) nbdig=0 numberentered={} initimg = np.zeros((dimtabx,dimtaby,3), np.uint8) slicenumberold=0 tlold=0 viewaskedold={} for keyne in usedclassif: viewaskedold[keyne]=False datav = np.zeros((500,900,3), np.uint8) imgtext = np.zeros((dimtabx,dimtaby,3), np.uint8) while(1): imgwip = np.zeros((200,200,3), np.uint8) cv2.setMouseCallback('imagepredict',draw_circle,img) c = cv2.getTrackbarPos('Contrast','Sliderfi') l = cv2.getTrackbarPos('Brightness','Sliderfi') tl = cv2.getTrackbarPos('Threshold','Sliderfi') fld = cv2.getTrackbarPos('Flip','Sliderfi') allview = cv2.getTrackbarPos('All','Sliderfi') noneview = cv2.getTrackbarPos('None','Sliderfi') fl=slnroi[fld] key = cv2.waitKey(1000) # if key != -1: # print key if key >47 and key<58: numberfinal=0 knum=key-48 # print 'this is number',knum numberentered[nbdig]=knum nbdig+=1 for i in range (nbdig): numberfinal=numberfinal+numberentered[i]*10**(nbdig-1-i) # print numberfinal numberfinal = min(slnt-1,numberfinal) if numberfinal>0: writeslice(numberfinal,initimg) if nbdig>0 and key ==8: numberfinal=0 nbdig=nbdig-1 for i in range (nbdig): numberfinal=numberfinal+numberentered[i]*10**(nbdig-1-i) # print numberfinal if numberfinal>0: writeslice(numberfinal,initimg) else: cv2.rectangle(initimg, (5,60), (150,50), black, -1) if nbdig>0 and key ==13 and numberfinal>0 : if numberfinal in slnroi: # print numberfinal numberfinal = min(slnt-1,numberfinal) # writeslice(numberfinal,initimg) cv2.rectangle(initimg, (5,60), (150,50), black, -1) fld=slnroi.index(numberfinal) # print fl,numberfinal cv2.setTrackbarPos('Flip','Sliderfi' ,fld) else: print 'number not in set' # cv2.rectangle(initimg, (5,60), (150,50), black, -1) cv2.rectangle(initimg, (5,60), (150,50), red, -1) cv2.putText(initimg,'NO ROI slice '+str(numberfinal)+'!',(5,60),cv2.FONT_HERSHEY_PLAIN,0.7,white,1 ) # cv2.putText(initimg, 'NO ROI on this slice',(5,60),cv2.FONT_HERSHEY_PLAIN,5,red,2,cv2.LINE_AA) # time.sleep(5) # fl=numberfinal numberfinal=0 nbdig=0 numberentered={} if key==2424832: fld=max(0,fld-1) cv2.setTrackbarPos('Flip','Sliderfi' ,fld) if key==2555904: fld=min(lenlimage-1,fld+1) cv2.setTrackbarPos('Flip','Sliderfi' ,fld) if allview==1: for key2 in usedclassif: cv2.setTrackbarPos(key2,'Sliderfi' ,1) cv2.setTrackbarPos('All','Sliderfi' ,0) if noneview==1: for key2 in usedclassif: cv2.setTrackbarPos(key2,'Sliderfi' ,0) cv2.setTrackbarPos('None','Sliderfi' ,0) for key2 in usedclassif: s = cv2.getTrackbarPos(key2,'Sliderfi') if s==1: viewasked[key2]=True else: viewasked[key2]=False slicenumber=fl # print slicenumber imagel=os.path.join(pdirk,list_image[slicenumber]) img = cv2.imread(imagel,1) img=cv2.resize(img,(dimtaby,dimtabx),interpolation=cv2.INTER_LINEAR) # print img.shape # print initimg.shape img=cv2.add(img,initimg) imglumi=lumi(img,l) imcontrast=contrasti(imglumi,c) imcontrast=cv2.cvtColor(imcontrast,cv2.COLOR_BGR2RGB) drawok=False if slicenumber != slicenumberold: slicenumberold=slicenumber drawok=True if tl != tlold: tlold=tl drawok=True for keyne in usedclassif: if viewasked[keyne]!=viewaskedold[keyne]: viewaskedold[keyne]=viewasked[keyne] # print 'change' drawok=True if drawok: # print 'view' imgtext = np.zeros((dimtabx,dimtaby,3), np.uint8) cv2.putText(imgwip,'WIP',(10,10),cv2.FONT_HERSHEY_PLAIN,5,red,2,cv2.LINE_AA) cv2.imshow('wip',imgwip) imgn,datav= drawpatch(tl,dimtabx,dimtaby,slicenumber,viewasked,patch_list_cross_slice, volumeroilocal,slnt,tabroi,num_class,tabscanLung) cv2.putText(datav,'slice number :'+str(slicenumber),(10,210),cv2.FONT_HERSHEY_PLAIN,0.7,white,1) cv2.putText(datav,'patient Name :'+tail,(10,220),cv2.FONT_HERSHEY_PLAIN,0.7,white,1) cv2.putText(datav,'CNN weight: '+cnnweigh,(10,230),cv2.FONT_HERSHEY_PLAIN,0.7,white,1) cv2.putText(datav,viewstyle,(10,200),cv2.FONT_HERSHEY_PLAIN,0.7,white,1) cv2.destroyWindow("wip") imgngray = cv2.cvtColor(imgn,cv2.COLOR_BGR2GRAY) np.putmask(imgngray,imgngray>0,255) mask_inv = cv2.bitwise_not(imgngray) outy=cv2.bitwise_and(imcontrast,imcontrast,mask=mask_inv) imgt=cv2.add(imgn,outy) dxrect=(dimtaby/2) cv2.rectangle(imgt,(dxrect,dimtabx-30),(dxrect+20,dimtabx-10),red,-1) cv2.putText(imgt,'quit',(dxrect+10,dimtabx-10),cv2.FONT_HERSHEY_PLAIN,1,yellow,1,cv2.LINE_AA) if patchi : cv2.putText(imgwip,'WIP',(10,10),cv2.FONT_HERSHEY_PLAIN,5,red,2,cv2.LINE_AA) cv2.imshow('wip',imgwip) # print 'retrieve patch asked' imgtext= retrievepatch(ix,iy,slicenumber,dimtabx,dimtaby,patch_list_cross_slice) patchi=False cv2.destroyWindow("wip") imgtoshow=cv2.add(imgt,imgtext) imgtoshow=cv2.cvtColor(imgtoshow,cv2.COLOR_BGR2RGB) imsstatus=cv2.getWindowProperty('Sliderfi', 0) imistatus= cv2.getWindowProperty('imagepredict', 0) imdstatus=cv2.getWindowProperty('datavisu', 0) # print imsstatus,imistatus,imdstatus if (imdstatus==0) and (imsstatus==0) and (imistatus==0) : cv2.imshow('imagepredict',imgtoshow) cv2.imshow('datavisu',datav) else: quitl=True if quitl or cv2.waitKey(20) & 0xFF == 27 : break quitl=False cv2.destroyWindow("imagepredict") cv2.destroyWindow("Sliderfi") cv2.destroyWindow("datavisu") return ''
cv2.imshow(state.WIN_NAME, out) key = cv2.waitKey(1) if key == ord("r"): state.reset() if key == ord("p"): state.paused ^= True if key == ord("d"): state.decimate = (state.decimate + 1) % 3 decimate.set_option(rs.option.filter_magnitude, 2 ** state.decimate) if key == ord("z"): state.scale ^= True if key == ord("c"): state.color ^= True if key == ord("s"): cv2.imwrite('./out.png', out) if key == ord("e"): points.export_to_ply('./out.ply', mapped_frame) if key in (27, ord("q")) or cv2.getWindowProperty(state.WIN_NAME, cv2.WND_PROP_AUTOSIZE) < 0: break # Stop streaming pipeline.stop()
import cv2 import numpy as np cap = cv2.VideoCapture(0) screen = np.zeros((800, 800, 3), np.uint8) while True: re, frame = cap.read() frame = cv2.resize(frame, None, fx=0.5, fy=0.5) screen[0:480, 0:640] = frame cv2.imshow('screen', screen) cv2.imshow('frame', frame) print(type(frame[0, 0][0])) print(cv2.getWindowProperty('screen', cv2.WND_PROP_VISIBLE)) if cv2.waitKey(1) & 0xff == ord('q'): break cv2.destroyAllWindows() cap.release()
def is_fullscreen(self): return cv2.getWindowProperty(self.name, cv2.WND_PROP_FULLSCREEN) == cv2.WINDOW_FULLSCREEN
def display(self, cap, functions, window, values): while cap.isOpened() and cv2.getWindowProperty( 'Slider', 0) >= 0 and cv2.getWindowProperty('Image', 0) >= 0: ret, img = cap.read() height, width = img.shape[:2] #image = image[height/3:height/2, :width/2] #image = img[:height, :width/2] images = [img[:height, :width / 2], img[:height, width / 2:width]] height, width = img.shape[:2] #image = image[height/3:height/3+15, :width/2] #image = image[height/3:height/2, :width/2] # read trackbar positions for each trackbar Hl = cv2.getTrackbarPos(values[0], window) Sl = cv2.getTrackbarPos(values[1], window) Vl = cv2.getTrackbarPos(values[2], window) Hh = cv2.getTrackbarPos(values[3], window) Sh = cv2.getTrackbarPos(values[4], window) Vh = cv2.getTrackbarPos(values[5], window) """ imageStrips = [img[int((2/9.0)*height):int((3/9.0)*height), :width], #np.zeros((2,width,3), np.uint8), img[int((3/9.0)*height):int((4/9.0)*height), :width]] """ """ images = [img[int((3/10.0)*height):int((4/10.0)*height), :width], #np.zeros((2,width,3), np.uint8), img[int((4/10.0)*height):int((5/10.0)*height), :width]] """ for image in images: converted_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) #converted_image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV) #converted_image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB) lower_threshold = np.uint8([Hl, Sl, Vl]) upper_threshold = np.uint8([Hh, Sh, Vh]) mask = cv2.inRange(converted_image, lower_threshold, upper_threshold) masked_image = cv2.bitwise_and(image, image, mask=mask) imgray = cv2.cvtColor(masked_image, cv2.COLOR_BGR2GRAY) #ret, thresh = cv2.threshold(imgray, 127, 255, 0) blurred = cv2.GaussianBlur(imgray, (5, 5), 0) thresh = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY)[1] contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1] cv2.drawContours(masked_image, contours, -1, (0, 255, 0), 3) #cv2.line(image, (width/3, 0), (width/3, height), [0, 0, 255], 3) if len(contours) > 0: c = max(contours, key=cv2.contourArea) M = cv2.moments(c) if M["m00"] > 0: cX = int(M["m10"] / M["m00"]) cY = int(M["m01"] / M["m00"]) else: cX, cY = 0, 0 cv2.circle(masked_image, (cX, cY), 5, (0, 0, 255), -1) else: pass cv2.imshow('Image', masked_image) if cv2.waitKey(1) & 0xFF == ord('q'): break
cv2.namedWindow("Canny Edge") def onTrack(x): pass cv2.createTrackbar("Max", "Canny Edge", 0, 255, onTrack) cv2.createTrackbar("Min", "Canny Edge", 0, 255, onTrack) loopFlag = 1 mx = 0 mn = 0 edge = img while cv2.getWindowProperty( 'Canny Edge', 0 ) >= 0: # Need to figure out X on fish fig& loopFlag >=0:# and cv2.getWindowProperty('Fish Edges', 0) >= 0): mx = cv2.getTrackbarPos("Max", "Canny Edge") mn = cv2.getTrackbarPos("Min", "Canny Edge") edge = cv2.Canny(img, mn, mx) #loopFlag = cv2.getWindowProperty('Fish Edges', 0) cv2.imshow("Fish Edges", edge) if cv2.waitKey(1) & 0xff == ord('q') or cv2.waitKey(1) & 0xff == ord( 'd' ): #ord returns the unicode of that letter. 0xff makes it only look at #the last 8 bits of the wait key because it is 32bits and the incode is only 8 bits break
def color_tracking(drone_vision:DroneVisionGUI, bebop:Bebop): def show_hist(hist): """Takes in the histogram, and displays it in the hist window.""" bin_count = hist.shape[0] bin_w = 24 img = np.zeros((256, bin_count * bin_w, 3), np.uint8) for i in range(bin_count): h = int(hist[i]) cv2.rectangle(img, (i * bin_w + 2, 255), ((i + 1) * bin_w - 2, 255 - h), (int(180.0 * i / bin_count), 255, 255), -1) img = cv2.cvtColor(img, cv2.COLOR_HSV2BGR) cv2.imshow('hist', img) showBackProj = False showHistMask = False frame = drone_vision.get_latest_valid_picture() if frame is not None: (hgt, wid, dep) = frame.shape cv2.namedWindow('camshift') cv2.namedWindow('hist') cv2.moveWindow('hist', 700, 100) # Move to reduce overlap # Initialize the track window to be the whole frame track_window = (0, 0, wid, hgt) # # Initialize the histogram from the stored image # Here I am faking a stored image with just a couple of blue colors in an array # you would want to read the image in from the file instead histImage = np.array([[[110, 70, 50]], [[111, 128, 128]], [[115, 100, 100]], [[117, 64, 50]], [[117, 200, 200]], [[118, 76, 100]], [[120, 101, 210]], [[121, 85, 70]], [[125, 129, 199]], [[128, 81, 78]], [[130, 183, 111]]], np.uint8) histImage = cv2.imread('orange.jpg') histImage = cv2.cvtColor(histImage,cv2.COLOR_BGR2HSV) maskedHistIm = cv2.inRange(histImage, np.array((0., 60., 32.)), np.array((180., 255., 255.))) cv2.imshow("masked",maskedHistIm) cv2.imshow("histim",histImage) hist = cv2.calcHist([histImage], [0], maskedHistIm, [16], [0, 180]) cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX) hist = hist.reshape(-1) show_hist(hist) # start processing frames while cv2.getWindowProperty('camshift', 0) >= 0: frame = drone_vision.get_latest_valid_picture() vis = frame.copy() hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # convert to HSV mask = cv2.inRange(hsv, np.array((0., 60., 32.)), np.array((180., 255., 255.))) # eliminate low and high saturation and value values # The next line shows which pixels are being used to make the histogram. # it sets to black all the ones that are masked away for being too over or under-saturated if showHistMask: vis[mask == 0] = 0 prob = cv2.calcBackProject([hsv], [0], hist, [0, 180], 1) prob &= mask term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1) track_box, track_window = cv2.CamShift(prob, track_window, term_crit) print(track_box[1][0]*track_box[1][1]) if showBackProj: vis[:] = prob[..., np.newaxis] try: cv2.ellipse(vis, track_box, (0, 0, 255), 2) area = track_box[1][0]*track_box[1][1] if area > 7000: print("GOING BACK") bebop.fly_direct(roll=0,pitch=-20,yaw=0,vertical_movement=0,duration=0.5) #bebop.smart_sleep(1) elif area < 4000: print("GOING FORWARD") bebop.fly_direct(roll=0,pitch=20,yaw=0,vertical_movement=0,duration=0.5) #bebop.smart_sleep(1) except: pass # print("Track box:", track_box) cv2.imshow('camshift', vis) ch = chr(0xFF & cv2.waitKey(5)) if ch == 'q': break elif ch == 'b': showBackProj = not showBackProj elif ch == 'v': showHistMask = not showHistMask bebop.safe_land(10) cv2.destroyAllWindows()
def show_camera(): max_index = 10 max_value = 0.0 last_value = 0.0 dec_count = 0 focal_distance = 10 focus_finished = False # To flip the image, modify the flip_method parameter (0 and 2 are the most common) print gstreamer_pipeline(flip_method=0) cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER) focusing(focal_distance) skip_frame = 2 if cap.isOpened(): window_handle = cv2.namedWindow('CSI Camera', cv2.WINDOW_AUTOSIZE) # Window while cv2.getWindowProperty('CSI Camera', 0) >= 0: ret_val, img = cap.read() cv2.imshow('CSI Camera', img) if skip_frame == 0: skip_frame = 3 if dec_count < 6 and focal_distance < 1000: #Adjust focus focusing(focal_distance) #Take image and calculate image clarity val = laplacian(img) #Find the maximum image clarity if val > max_value: max_index = focal_distance max_value = val #If the image clarity starts to decrease if val < last_value: dec_count += 1 else: dec_count = 0 #Image clarity is reduced by six consecutive frames if dec_count < 6: last_value = val #Increase the focal distance focal_distance += 10 elif not focus_finished: #Adjust focus to the best focusing(max_index) focus_finished = True else: skip_frame = skip_frame - 1 # This also acts as keyCode = cv2.waitKey(16) & 0xff # Stop the program on the ESC key if keyCode == 27: break elif keyCode == 10: max_index = 10 max_value = 0.0 last_value = 0.0 dec_count = 0 focal_distance = 10 focus_finished = False cap.release() cv2.destroyAllWindows() else: print 'Unable to open camera'
def read_cam(): cap = cv2.VideoCapture(0) if cap.isOpened(): windowName = "Edge Detection" cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) cv2.resizeWindow(windowName, 1280, 720) cv2.moveWindow(windowName, 0, 0) cv2.setWindowTitle(windowName, "InfraRed Stereo") cap.set(cv2.CAP_PROP_CONVERT_RGB, False) showWindow = 3 # Show all stages showHelp = True edgeThreshold = 40 showFullScreen = False while True: if cv2.getWindowProperty(windowName, 0) < 0: break; ret_val, frame = cap.read(); IR_L, BGGR_L = conversion(frame) hsv = cv2.cvtColor( BGGR_L, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(hsv, (7, 7), 1.5) edges = cv2.Canny(blur, 0, edgeThreshold) if showWindow == 3: frameRs = cv2.resize( BGGR_L, (640, 360)) hsvRs = cv2.resize(hsv, (640, 360)) vidBuf = np.concatenate((frameRs, cv2.cvtColor(hsvRs, cv2.COLOR_GRAY2BGR)), axis=1) blurRs = cv2.resize(blur, (640, 360)) edgesRs = cv2.resize(edges, (640, 360)) vidBuf1 = np.concatenate( (cv2.cvtColor(blurRs, cv2.COLOR_GRAY2BGR), cv2.cvtColor(edgesRs, cv2.COLOR_GRAY2BGR)), axis=1) vidBuf = np.concatenate((vidBuf, vidBuf1), axis=0) if showWindow == 1: # Show Camera Frame displayBuf = BGGR_L elif showWindow == 2: # Show Canny Edge Detection displayBuf = edges elif showWindow == 3: # Show All Stages displayBuf = vidBuf cv2.imshow(windowName, displayBuf) key = cv2.waitKey(10) if key == 27: # Check for ESC key cv2.destroyAllWindows() break; elif key == 49: # 1 key, show frame cv2.setWindowTitle(windowName, "Camera Feed") showWindow = 1 elif key == 50: # 2 key, show Canny cv2.setWindowTitle(windowName, "Canny Edge Detection") showWindow = 2 elif key == 51: # 3 key, show Stages cv2.setWindowTitle(windowName, "Camera, Gray scale, Gaussian Blur, Canny Edge Detection") showWindow = 3 elif key == 52: # 4 key, toggle help showHelp = not showHelp elif key == 44: # , lower canny edge threshold edgeThreshold = max(0, edgeThreshold - 1) print('Canny Edge Threshold Maximum: ', edgeThreshold) elif key == 46: # , raise canny edge threshold edgeThreshold = edgeThreshold + 1 print('Canny Edge Threshold Maximum: ', edgeThreshold) elif key == 74: # Toggle fullscreen; This is the F3 key on this particular keyboard # Toggle full screen mode if showFullScreen == False: cv2.setWindowProperty(windowName, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) else: cv2.setWindowProperty(windowName, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_NORMAL) showFullScreen = not showFullScreen else: print ("camera open failed")
img_width = frame.shape[1] left_roi = int(img_width / 5) * 2 right_roi = img_width - left_roi top_roi = int(img_height / 5) * 2 bottom_roi = img_height - top_roi millis = lambda: int(round(time.time() * 1000)) previousTime = 0 deltaTime = 0 counter = 1 time_acc = 0 while (not want_to_exit and cv2.getWindowProperty(win_name, 0) >= 0): ## Gestion de temps currentTime = millis() deltaTime = currentTime - previousTime previousTime = currentTime time_acc += deltaTime ## Gestion de la lecture retval, frame = cap.read() ## Pipeline img_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) img_diff = cv2.absdiff(img_gray, template)
def main(): global gn_input_queue, gn_output_queue, ty_proc, gn_proc_list,\ resize_output, resize_output_width, resize_output_height, video_proc, video_queue if (not handle_args()): # invalid arguments, print usage info and exit program print_usage() return 1 # get list of all the .mp4 files in the image directory input_video_filename_list = os.listdir(input_video_path) input_video_filename_list = [i for i in input_video_filename_list if i.endswith('.mp4')] if (len(input_video_filename_list) < 1): # no images to show print('No video (.mp4) files found') return 1 # print keyboard mapping to console so user will know what can be adjusted. print_info() # Set logging level and initialize/open the first NCS we find mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 0) devices = mvnc.EnumerateDevices() if len(devices) < 2: print('This application requires two NCS devices.') print('Insert two devices and try again!') return 1 # use the first NCS device for tiny yolo processing ty_device = mvnc.Device(devices[0]) ty_device.OpenDevice() # use the rest of the NCS devices for googlenet processing if (not init_gn_lists(devices[1:], gn_proc_list, gn_device_list)): print('Error initializing NCS devices for GoogleNet') return 1 print ('Using ' + str(len(gn_proc_list)) + ' NCS devices for GoogLeNet') print('Starting GUI, press Q to quit') # create the GUI window cv2.namedWindow(cv_window_name) cv2.moveWindow(cv_window_name, 10, 10) cv2.waitKey(1) # Queue of video frame images which will be the output for the video processor video_queue = queue.Queue(VIDEO_QUEUE_SIZE) # Setup tiny_yolo_processor that reads from the video queue # and writes to its own ty_output_queue ty_output_queue = queue.Queue(TY_OUTPUT_QUEUE_SIZE) ty_proc = tiny_yolo_processor(TINY_YOLO_GRAPH_FILE, ty_device, video_queue, ty_output_queue, TY_INITIAL_BOX_PROBABILITY_THRESHOLD, TY_INITIAL_MAX_IOU, QUEUE_WAIT_MAX, QUEUE_WAIT_MAX) exit_app = False while (True): for input_video_file in input_video_filename_list : # clear all the queues for the cases where this isn't the # first video in the list. video_queue.queue.clear() ty_output_queue.queue.clear() gn_input_queue.queue.clear() gn_output_queue.queue.clear() # video processor that will put video frames images on the video_queue video_proc = video_processor(video_queue, input_video_path + '/' + input_video_file, VIDEO_QUEUE_PUT_WAIT_MAX, VIDEO_QUEUE_FULL_SLEEP_SECONDS) for gn_proc in gn_proc_list: gn_proc.start_processing() video_proc.start_processing() ty_proc.start_processing() frame_count = 0 start_time = time.time() end_time = start_time total_paused_time = end_time - start_time while True : try: (display_image, filtered_objs) = ty_output_queue.get(True, QUEUE_WAIT_MAX) except : pass get_googlenet_classifications(display_image, filtered_objs) #get_googlenet_classifications_no_queue(gn_proc_list[0], display_image, filtered_objs) # check if the window is visible, this means the user hasn't closed # the window via the X button prop_val = cv2.getWindowProperty(cv_window_name, cv2.WND_PROP_ASPECT_RATIO) if (prop_val < 0.0): end_time = time.time() ty_output_queue.task_done() exit_app = True print('window closed') break overlay_on_image(display_image, filtered_objs) if (resize_output): display_image = cv2.resize(display_image, (resize_output_width, resize_output_height), cv2.INTER_LINEAR) # update the GUI window with new image cv2.imshow(cv_window_name, display_image) ty_output_queue.task_done() raw_key = cv2.waitKey(1) if (raw_key != -1): if (handle_keys(raw_key) == False): end_time = time.time() exit_app = True print('user pressed Q') break if (pause_mode): pause_start = time.time() while (pause_mode): raw_key = cv2.waitKey(1) if (raw_key != -1): if (handle_keys(raw_key) == False): end_time = time.time() do_unpause() exit_app = True print('user pressed Q during pause') break if (exit_app): break; pause_stop = time.time() total_paused_time = total_paused_time + (pause_stop - pause_start) frame_count = frame_count + 1 if (video_queue.empty()): end_time = time.time() print('video queue empty') break frames_per_second = frame_count / ((end_time - start_time) - total_paused_time) print('Frames per Second: ' + str(frames_per_second)) video_proc.stop_processing() video_proc.cleanup() cv2.waitKey(1) ty_proc.stop_processing() cv2.waitKey(1) for gn_proc in gn_proc_list: cv2.waitKey(1) gn_proc.stop_processing() if (exit_app) : break if (exit_app) : break # clean up tiny yolo ty_proc.cleanup() ty_device.CloseDevice() # Clean up googlenet for gn_index in range(0, len(gn_proc_list)): cv2.waitKey(1) gn_proc_list[gn_index].cleanup() gn_device_list[gn_index].CloseDevice() print('Finished')
if passthrough: out = frame elif is_calibrated: tt.tic() out = predictor.predict(frame) if out is None: log('predict returned None') timing['predict'] = tt.toc() else: out = None tt.tic() key = cv2.waitKey(1) if cv2.getWindowProperty('cam', cv2.WND_PROP_VISIBLE) < 1.0: break elif is_calibrated and cv2.getWindowProperty( 'avatarify', cv2.WND_PROP_VISIBLE) < 1.0: break if key == 27: # ESC break elif key == ord('d'): cur_ava += 1 if cur_ava >= len(avatars): cur_ava = 0 passthrough = False change_avatar(predictor, avatars[cur_ava]) elif key == ord('a'): cur_ava -= 1
def isWindowOpen(name): return cv2.getWindowProperty(name, cv2.WND_PROP_AUTOSIZE) != -1
def main(): # for criterion, minval can be any exorbitantly large value minval = 500000000 quota = 0 camera = cv2.VideoCapture(camera_port) for i in xrange(ramp_frames): retval, temp = camera.read() while True: start_time = time.time() # The function waitKey waits for a key event infinitely (when delay<=0 ) # or for delay milliseconds, when it is positive. It returns the code of # the pressed key or -1 if no key was pressed before the specified time # had elapsed. Escape code is 27 wk = cv2.cv.WaitKey(10) print wk # registration criterion, only grade if we've improved 7 times # break on space bar if quota == 20 or wk == 32: # if registered exists, try grading it. otherwise, break # if reg exists and you grade it successfully, break. o/w keep going if 'registered' in locals(): try: print 'grading...' if not grade(registered): cv2.imwrite('misaligned.jpg', blend_visual) quota = 0 else: # we stop only when we get a complete grade break except: if not suppress: print "something bad is happening at Grade" else: break # escapes if wk == 27: # escape break # dynamic views, there's a better structure for this if wk == 114: # r views['reg'] = not views['reg'] if wk == 99: # c views['corners'] = not views['corners'] if wk == 101: # e views['edges'] = not views['edges'] if wk == 116: # t views['thresh'] = not views['thresh'] if wk == 98: # b views['blend'] = not views['blend'] if wk == 118: # v views['visual'] = not views['visual'] # output control if wk == 115: debug['suppress'] = not debug['suppress'] retval, im = camera.read() try: im = np.array(im[:, :im.shape[0], :]) im = cv2.resize(im, win) # present feed for input draw_square(im) cv2.imshow('input', cv2.flip(im, 1)) gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray, 100, 240) region(edges, margin=0) toggle_view('edges', edges) intersections = findPoints(edges) except Errors.ImproperIntersectionsError as e: if not suppress: print e continue except Exception as e: if not suppress: print e, traceback.format_exception(*sys.exc_info()) continue # if no points found, this code isn't seen # show computed intersection points for point in intersections: cv2.circle(im, (int(round(point[0])), int(round(point[1]))), 6, (0, 0, 255), -1) toggle_view('corners', cv2.flip(im, 1)) try: registered = transform(im, intersections) toggle_view('reg', registered) except Errors.NotEnoughPointsToTransformError as e: if not suppress: print e continue # adjust registered registered = cv2.cvtColor(registered, cv2.COLOR_BGR2GRAY) retval, registered_bin = cv2.threshold(registered, 120, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU) '''==========BLENDING AND REGISTRATION CRITERION==========''' # ----------------------------- # This is for visual comparison # ----------------------------- if views['visual']: # template to show difference between it and the registered image # this read incurs a 5-fold decrease in speed template_visual = cv2.cvtColor(cv2.imread('blank.jpg'), cv2.COLOR_BGR2GRAY) # reduce to dimensions to match registered template_visual = cv2.resize(template_visual, (0, 0), fx=0.25, fy=0.25) blend_visual = cv2.addWeighted(registered, .7, template_visual, .3, 0) cv2.imshow('visual blend', blend_visual) elif cv2.getWindowProperty('visual blend', cv2.CV_WINDOW_AUTOSIZE) > 0: # get any window prop to check for existence cv2.destroyWindow('visual blend') # ----------------------------------- # This is for mathematical comparison # ----------------------------------- # create criterion template, reverse size tuple template_math = np.zeros(myDims[::-1], dtype=np.uint8) template_math = ndrawAnswerCoords(template_math) # compute blend blend_math = cv2.bitwise_and(registered_bin, template_math) # impose criterion blend_sum = np.sum(blend_math) toggle_view('blend', blend_math) if blend_sum < minval: # take {quote} improvements before showing grade print blend_sum quota += 1 minval = blend_sum print "time: ", time.time()-start_time '''=======================================================''' cv2.cv.DestroyAllWindows() camera.release()
def my_link(): calm = "" FinalEmotion = "" count = 0 heartRateNeutralStart = 80 heartRateNeutralEnd = 100 heartRateDummyValue = 110 #EMOTIONS and their corresponding FRAGRANCES angryFragrance = "rose" happyFragrance = "lavender" sadFragrance = "woods" surprisedFragrance = "citrus" calmFragrance = "ocean" #DEFAULT USER PREFERENCES Sophie = "lavender" Claire = "ocean" Josephine = "ocean" Benoit = "rose" Faustine = "woods" Harvey = "citrus" Emile = "rose" CLIENT_ID = '22BQWT' CLIENT_SECRET = 'dfc33d81a41114e8feff6d022216842e' values_dict = { 'angry': 'angry', 'happy': 'happy', 'surprised': 'surprised', 'sad': 'sad' } question = request.form['questionnaire'] userName = request.form['userName'] print("userName = "******"question =", question) # ********************GOOGLE VISION************************************* #open('pri.txt', 'w').close() #Emotions emo = ['Angry', 'Surprised', 'Sad', 'Happy'] likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE', 'LIKELY', 'VERY_LIKELY') ############## Spanish version ################# #emo = ['Bravo', 'Sorprendido','Triste', 'Feliz'] #string = 'Sin emocion' #from google.oauth2 import service_account #credentials = service_account.Credentials.from_service_account_file('key.json') os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "E:\Flask_Web_app\key.json" # Instantiates a client vision_client = vision.ImageAnnotatorClient() cv2.imshow('Video', np.empty((5, 5), dtype=float)) compressRate = 1 while cv2.getWindowProperty('Video', 0) >= 0: video_capture = cv2.VideoCapture(0) ret, img = video_capture.read() img = cv2.resize(img, (0, 0), fx=compressRate, fy=compressRate) ok, buf = cv2.imencode(".jpeg", img) image = vision.types.Image(content=buf.tostring()) response = vision_client.face_detection(image=image) faces = response.face_annotations len(faces) for face in faces: x = face.bounding_poly.vertices[0].x y = face.bounding_poly.vertices[0].y x2 = face.bounding_poly.vertices[2].x y2 = face.bounding_poly.vertices[2].y cv2.rectangle(img, (x, y), (x2, y2), (0, 255, 0), 2) sentiment = [ likelihood_name[face.anger_likelihood], likelihood_name[face.surprise_likelihood], likelihood_name[face.sorrow_likelihood], likelihood_name[face.joy_likelihood] ] #likelihood_name[face.under_exposed_likelihood], #likelihood_name[face.blurred_likelihood], #likelihood_name[face.headwear_likelihood]] with open("pri.txt", "a") as text_file: for item, item2 in zip(emo, sentiment): print(item, ":", item2, file=text_file) string = 'No sentiment' if not (all(item == 'VERY_UNLIKELY' for item in sentiment)): if any(item == 'VERY_LIKELY' for item in sentiment): state = sentiment.index('VERY_LIKELY') # the order of enum type Likelihood is: #'LIKELY', 'POSSIBLE', 'UNKNOWN', 'UNLIKELY', 'VERY_LIKELY', 'VERY_UNLIKELY' # it makes sense to do argmin if VERY_LIKELY is not present, one would espect that VERY_LIKELY # would be the first in the order, but that's not the case, so this special case must be added else: state = np.argmin(sentiment) string = emo[state] cv2.putText(img, string, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2) cv2.imshow("Video", img) cv2.waitKey(1) video_capture.release() # When everything is done, release the capture cv2.destroyAllWindows() dictList = [] with open('pri.txt', 'r') as f: for line in f: elements = line.rstrip().split(":") dictList.append(dict(zip(elements[::2], elements[1::2]))) for i in range(0, len(dictList)): for key in dictList[i]: varx = dictList[i][key].lstrip() dictList[i][key] = varx dictnew = {} for k, v in [(key, d[key]) for d in dictList for key in d]: if k not in dictnew: dictnew[k] = [v] else: dictnew[k].append(v) def counter(dict1): dictnew = { 'Angry ': [{ 'VERY_LIKELY': 0 }, { 'LIKELY': 0 }, { 'POSSIBLE': 0 }, { 'UNLIKELY': 0 }, { 'VERY_UNLIKELY': 0 }], 'Surprised ': [{ 'VERY_LIKELY': 0 }, { 'LIKELY': 0 }, { 'POSSIBLE': 0 }, { 'UNLIKELY': 0 }, { 'VERY_UNLIKELY': 0 }], 'Sad ': [{ 'VERY_LIKELY': 0 }, { 'LIKELY': 0 }, { 'POSSIBLE': 0 }, { 'UNLIKELY': 0 }, { 'VERY_UNLIKELY': 0 }], 'Happy ': [{ 'VERY_LIKELY': 0 }, { 'LIKELY': 0 }, { 'POSSIBLE': 0 }, { 'UNLIKELY': 0 }, { 'VERY_UNLIKELY': 0 }] } for key in dict1.keys(): for k in dictnew.keys(): if key == k: for j in range(0, len(dict1[key])): for m in range(0, 5): for n in dictnew[key][m].keys(): if n == dict1[key][j]: dictnew[key][m][n] = dictnew[key][m][n] + 1 for k in dictnew.keys(): dictnew[k][4]['VERY_UNLIKELY'] = 0 return dictnew dictnew = counter(dictnew) def score(dict1): order = ['VERY_LIKELY', 'LIKELY', 'POSSIBLE', 'UNLIKELY'] scoredict = {'Angry ': 0, 'Surprised ': 0, 'Sad ': 0, 'Happy ': 0} for key in dict1.keys(): for j in range(0, len(dict1[key])): for n in dictnew[key][j].keys(): if dictnew[key][j][n] > 0: scoredict[key] = scoredict[key] + dictnew[key][j][n] listvar = [] var1 = 0 final = { 'Angry ': 'VERY_UNLIKELY', 'Surprised ': 'VERY_UNLIKELY', 'Sad ': 'VERY_UNLIKELY', 'Happy ': 'VERY_UNLIKELY' } for k in scoredict.keys(): if scoredict[k] > var1: listvar.clear() listvar.append({k: scoredict[k]}) var1 = scoredict[k] elif scoredict[k] == var1: listvar.append({k: scoredict[k]}) semi = [] varneed = 0 for p in range(0, len(listvar)): for kites in listvar[p].keys(): for key in dict1.keys(): if key == kites: for l in range(0, 4): for locks in dict1[key][l].keys(): if dict1[key][l][locks] > 0: if len(semi) > 0: for z in semi[0].keys(): if order.index( semi[0][z]) > order.index( locks): semi.pop() semi.append({key: locks}) else: semi.append({key: locks}) for keys in semi[0].keys(): final[keys] = semi[0][keys] for key, value in final.items(): if (key == "Angry "): angry = value values_dict["angry"] = angry if (key == "Surprised "): surprised = value values_dict["surprised"] = surprised if (key == "Sad "): sad = value values_dict["sad"] = sad if (key == "Happy "): happy = value values_dict["happy"] = happy return (scoredict, listvar, final) scoredict, listvar, final = score(dictnew) #open('pri.txt', 'w').close() #*******************FITBIT******************************* #Server authentication server = Oauth2.OAuth2Server(CLIENT_ID, CLIENT_SECRET) server.browser_authorize() ACCESS_TOKEN = str(server.fitbit.client.session.token['access_token']) REFRESH_TOKEN = str(server.fitbit.client.session.token['refresh_token']) auth2_client = fitbit.Fitbit(CLIENT_ID, CLIENT_SECRET, oauth2=True, access_token=ACCESS_TOKEN, refresh_token=REFRESH_TOKEN) yesterday = str((datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%Y%m%d")) yesterday2 = str((datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%Y-%m-%d")) today = str(datetime.datetime.now().strftime("%Y-%m-%d")) fit_statsHR = auth2_client.intraday_time_series('activities/heart', base_date=today, detail_level='1sec') time_list = [] val_list = [] for i in fit_statsHR['activities-heart-intraday']['dataset']: val_list.append(i['value']) time_list.append(i['time']) heartdf = pd.DataFrame({'Heart Rate': val_list, 'Time': time_list}) if len(val_list) == 0: heartRate = heartRateDummyValue #if FitBit is not synced today then the list is empty.Value 90 assigned to heartRate if fitbit is not synced today. else: heartRate = val_list[ -1] #Latest value from fitbit app is assigned to heartRate print(f'HeartRate={heartRate}') #Prints heartRate in Terminal #***********************ALGORITHM************************************ #assign heartRate if (heartRate < heartRateNeutralStart): heartBeat = "below" elif (heartRate > heartRateNeutralEnd): heartBeat = "above" else: heartBeat = "neutral" for emotion, value in values_dict.items(): print(emotion, " = ", value) if (value == "VERY_LIKELY" ): #1st branch of algorithm-one emotion is VERY_LIKELY FinalEmotion = emotion if (FinalEmotion == "" ): #3rd branch of algorithm-all emotions are VERY_UNLIKELY for emotion, value in values_dict.items(): if (value == "VERY_UNLIKELY"): count += 1 if (count == 4): if (heartBeat == "above"): if (question == "left"): FinalEmotion = "angry" elif (question == "right"): FinalEmotion = "happy" elif (question == "neutral"): FinalEmotion = "Print emotion from Emotion Table" elif (question == "angry"): FinalEmotion = "angry" elif (question == "happy"): FinalEmotion = "happy" elif (question == "calm"): FinalEmotion = "calm" elif (question == "sad"): FinalEmotion = "sad" elif (heartBeat == "below"): if (question == "left"): FinalEmotion = "sad" elif (question == "right"): FinalEmotion = "calm" elif (question == "neutral"): FinalEmotion = "Print emotion from Emotion Table" elif (question == "angry"): FinalEmotion = "angry" elif (question == "happy"): FinalEmotion = "happy" elif (question == "calm"): FinalEmotion = "calm" elif (question == "sad"): FinalEmotion = "sad" elif (heartBeat == "neutral"): if (question == "left"): FinalEmotion = "Print emotion from Emotion Table" elif (question == "right"): FinalEmotion = "Print emotion from Emotion Table" elif (question == "neutral"): FinalEmotion = "Print emotion from Emotion Table" elif (question == "angry"): FinalEmotion = "angry" elif (question == "happy"): FinalEmotion = "happy" elif (question == "calm"): FinalEmotion = "calm" elif (question == "sad"): FinalEmotion = "sad" if (FinalEmotion == ""): #2nd branch of algorithm -neutral if (heartBeat == "above"): if (question == "left"): FinalEmotion = "angry" elif (question == "right"): FinalEmotion = "happy" elif (question == "neutral"): FinalEmotion = "Print emotion from Emotion Table" elif (question == "angry"): FinalEmotion = "angry" elif (question == "happy"): FinalEmotion = "happy" elif (question == "calm"): FinalEmotion = "calm" elif (question == "sad"): FinalEmotion = "sad" elif (heartBeat == "below"): if (question == "left"): FinalEmotion = "sad" elif (question == "right"): FinalEmotion = "calm" elif (question == "neutral"): FinalEmotion = "Print emotion from Emotion Table" elif (question == "angry"): FinalEmotion = "angry" elif (question == "happy"): FinalEmotion = "happy" elif (question == "calm"): FinalEmotion = "calm" elif (question == "sad"): FinalEmotion = "sad" elif (heartBeat == "neutral"): if (question == "left"): FinalEmotion = "Print emotion from Emotion Table" elif (question == "right"): FinalEmotion = "Print emotion from Emotion Table" elif (question == "neutral"): FinalEmotion = "Print emotion from Emotion Table" elif (question == "angry"): FinalEmotion = "angry" elif (question == "happy"): FinalEmotion = "happy" elif (question == "calm"): FinalEmotion = "calm" elif (question == "sad"): FinalEmotion = "sad" #prints final emotion in the terminal print(f'Final Emotion={FinalEmotion}') #Pop up window displays result popup = tk.Tk() popup.wm_title("Result") if (FinalEmotion == "angry"): label = ttk.Label(popup, text="Hi " + userName + "!\n You are angry !\n Enjoy the " + angryFragrance + " fragrance!") elif (FinalEmotion == "happy"): label = ttk.Label(popup, text="Hi " + userName + "!\n You are happy!\n Enjoy the " + happyFragrance + " fragrance!") elif (FinalEmotion == "calm"): label = ttk.Label(popup, text="Hi " + userName + "!\n You are calm!\n Enjoy the " + calmFragrance + " fragrance!") elif (FinalEmotion == "sad"): label = ttk.Label(popup, text="Hi " + userName + "!\n You are sad!\n Enjoy the " + sadFragrance + " fragrance!") elif (FinalEmotion == "Print emotion from Emotion Table"): if (userName == "Sophie"): label = ttk.Label( popup, text="Hi " + userName + "! \n You are neutral.\n Enjoy your prefered fragrance " + Sophie + " !") elif (userName == "Claire"): label = ttk.Label( popup, text="Hi " + userName + "! \n You are neutral.\n Enjoy your prefered fragrance " + Claire + " !") elif (userName == "Josephine"): label = ttk.Label( popup, text="Hi " + userName + "! \n You are neutral.\n Enjoy your prefered fragrance " + Josephine + " !") elif (userName == "Benoit"): label = ttk.Label( popup, text="Hi " + userName + "! \n You are neutral.\n Enjoy your prefered fragrance " + Benoit + " !") elif (userName == "Faustine"): label = ttk.Label( popup, text="Hi " + userName + "! \n You are neutral.\n Enjoy your prefered fragrance " + Faustine + " !") elif (userName == "Harvey"): label = ttk.Label( popup, text="Hi " + userName + "! \n You are neutral.\n Enjoy your prefered fragrance " + Harvey + " !") elif (FinalEmotion == "surprised"): label = ttk.Label(popup, text="Hi \n You are surprised!\n Enjoy the " + surprisedFragrance + " fragrance!") else: label = ttk.Label(popup, text="test case") label.pack(side="top", fill="x", pady=10) B1 = ttk.Button(popup, text="ok", command=popup.destroy) B1.pack() popup.mainloop() #Retains and displays the web page #arduino code starts # initialize connection to Arduino # if your arduino was running on a serial port other than '/dev/ttyACM0/' # declare: a = Arduino(serial_port='/dev/ttyXXXX') a = Arduino() time.sleep(3) # declare the pins we're using LED_PIN = 50 ANALOG_PIN = 0 emotion = 50 # initialize the digital pin as output a.set_pin_mode(emotion, 'O') print('Arduino initialized') # if we make a post request on the webpage aka press button then do stuff # if request.method == 'POST': # if we press the turn on button # print("emotion =", emotion) # if request.form['submit'] == 'start' : # # emotion = FinalEmotion if FinalEmotion == "angry": emotion = 42 ##enter the pin assigned elif FinalEmotion == "happy": emotion = 50 ##enter the pin assigned elif FinalEmotion == "sad": emotion = 48 ##enter the pin assigned elif FinalEmotion == "calm": emotion = 46 ##enter the pin assigned elif FinalEmotion == "surprised": emotion = 44 ##enter the pin assigned else: emotion = 46 ##enter the pin assigned print('TURN ON') try: #turn off the LED on arduino for i in range(0, 100): a.digital_write(i, 0) # turn on LED on arduino print("emotion inside catch type =", type(emotion)) print("emotion inside catch value =", emotion) # a.set_pin_mode(emotion,'O') a.digital_write(emotion, 1) except: print("already entered") return (''), 204
RED = (0,0,255) YELL = (0,255,255) #Filters path haar_faces = cv2.CascadeClassifier('./filters/haarcascade_frontalface_default.xml') haar_eyes = cv2.CascadeClassifier('./filters/haarcascade_eye.xml') haar_mouth = cv2.CascadeClassifier('./filters/Mouth.xml') haar_nose = cv2.CascadeClassifier('./filters/Nose.xml') #config WebCam video_capture = cv2.VideoCapture(0) cv2.imshow('Video', np.empty((5,5),dtype=float)) while cv2.getWindowProperty('Video', 0) >= 0: # Capture frame-by-frame ret, frame = video_capture.read() #frame = cv2.imread('faceswap/lol2.jpg') faces = apply_Haar_filter(frame, haar_faces, 1.3 , 5, 30) for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x+w, y+h), BLUE, 2) #blue sub_img = frame[y:y+h,x:x+w,:] eyes = apply_Haar_filter(sub_img, haar_eyes, 1.3 , 10, 10) for (x2, y2, w2, h2) in eyes: cv2.rectangle(frame, (x+x2, y+y2), (x + x2+w2, y + y2+h2), YELL, 2)
def run(self): cap = cv2.VideoCapture(0) detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor( 'shape_predictor_68_face_landmarks.dat') eyeFrame = np.zeros((150, 300, 3), np.uint8) coordinates = (0, 0) # screens cv2.namedWindow('eyeWindow') cv2.createTrackbar('threshold', 'eyeWindow', 0, 255, self.on_threshold) callibrationScreen = np.zeros((1080, 1920, 3), np.uint8) testYourTracking = np.zeros((1080, 1920, 3), np.uint8) detector_params = cv2.SimpleBlobDetector_Params() detector_params.filterByColor = True detector_params.blobColor = 255 #detector_params.filterByArea = True #detector_params.maxArea = 3000 blob_detector = cv2.SimpleBlobDetector_create(detector_params) while True: ret, img = cap.read() img = cv2.flip(img, 1) # img = cv2.resize(img, None, fx=0.5, fy=0.5) grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = detector(grayImg) if len(faces) != 0: for face in faces: # gets coordinates of the face facex, facey = face.left(), face.top() facex1, facey1 = face.right(), face.bottom() # draws a rectangle around the face cv2.rectangle(img, (facex, facey), (facex1, facey1), (0, 0, 255), 2) landmarks = predictor(grayImg, face) left_point = (landmarks.part(36).x, landmarks.part(36).y) right_point = (landmarks.part(39).x, landmarks.part(39).y) top_center = self.midpoint(landmarks.part(37), landmarks.part(38)) bottom_center = self.midpoint(landmarks.part(41), landmarks.part(40)) #Gaze Ratio left_eye_region = np.array( [(landmarks.part(36).x, landmarks.part(36).y), (landmarks.part(37).x, landmarks.part(37).y), (landmarks.part(38).x, landmarks.part(38).y), (landmarks.part(39).x, landmarks.part(39).y), (landmarks.part(40).x, landmarks.part(40).y), (landmarks.part(41).x, landmarks.part(41).y)], np.int32) min_x = np.min(left_eye_region[:, 0]) max_x = np.max(left_eye_region[:, 0]) min_y = np.min(left_eye_region[:, 1]) max_y = np.max(left_eye_region[:, 1]) eye = img[min_y - 1:max_y, min_x:max_x] eye = cv2.resize(eye, None, fx=3, fy=3) gray_eye = cv2.cvtColor(eye, cv2.COLOR_BGR2GRAY) threshold = cv2.getTrackbarPos('threshold', 'eyeWindow') _, eyeImg = cv2.threshold(gray_eye, threshold, 255, cv2.THRESH_BINARY_INV) keypoints = self.blob_process(eyeImg, blob_detector) # print(keypoints) for keypoint in keypoints: s = keypoint.size x = keypoint.pt[0] y = keypoint.pt[1] cx = int(x) cy = int(y) coordinates = (cx, cy) cv2.circle(eye, (cx, cy), 5, (0, 0, 255), -1) cv2.drawKeypoints( eye, keypoints, eye, (0, 255, 0), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) cv2.polylines(img, [left_eye_region], True, (255, 0, 255), 2) # print(eye.shape) ey, ex, ch = eye.shape eyeFrame[int(75 - (ey / 2)):int(75 + (ey / 2)), int(150 - (ex / 2)):int(150 + (ex / 2))] = eye # cv2.imshow('eyeWindow', eye) if showEyeWindow: cv2.imshow('eyeWindow', eyeFrame) else: if cv2.getWindowProperty('eyeWindow', cv2.WND_PROP_VISIBLE) != -1.0: cv2.destroyWindow('eyeWindow') if screenToggle % 2 == 1: global point cv2.putText(callibrationScreen, "UP", (900, 60), cv2.FONT_HERSHEY_COMPLEX, 2, (255, 255, 255), 2) cv2.putText(callibrationScreen, "RIGHT", (1700, 540), cv2.FONT_HERSHEY_COMPLEX, 2, (255, 255, 255), 2) cv2.putText(callibrationScreen, "LEFT", (20, 540), cv2.FONT_HERSHEY_COMPLEX, 2, (255, 255, 255), 2) cv2.putText(callibrationScreen, "DOWN", (860, 1000), cv2.FONT_HERSHEY_COMPLEX, 2, (255, 255, 255), 2) cv2.putText(callibrationScreen, "CENTER", (910, 520), cv2.FONT_HERSHEY_COMPLEX, 2, (255, 255, 255), 2) if click == True: if coordinates[ 0] == coordinates[1] & coordinates[0] == 0: pass else: points[point] = coordinates point = point + 1 print(points, point) if recallibrated == True: point = 1 cv2.setMouseCallback('Callibration Screen', self.click_pos) cv2.imshow('Callibration Screen', callibrationScreen) # the tracking screen if screenToggle % 2 == 0 & screenToggle != 0: cv2.destroyWindow('Callibration Screen') if showTrackingScreen: if point >= 5: ratioX = int((960 / points[5][0])) ratioY = int((540 / points[5][1])) pointerX = (coordinates[0] * (ratioX * 2)) pointerY = (coordinates[1] * (ratioY * 2)) # print(pointerX, pointerY) cv2.circle(testYourTracking, (pointerX, pointerY), 3, (0, 0, 255), -1) cv2.imshow('Test your Tracking', testYourTracking) else: if cv2.getWindowProperty('Test your Tracking', cv2.WND_PROP_VISIBLE) != -1.0: cv2.destroyWindow('Test Your Tracking') if ret: self.change_pixmap_signal.emit(img)
def update_frame(self, frame): """ Updates the frame by drawing the line and/or rectangle shape using the information stored from the user's clicks. Parameters ---------- frame : Cam current camera frame """ if self.model.return_i() > 10 and getWindowProperty('Bus-Factor', 0) < 0: kill(getpid(), 1) self.frame = frame self.draw_classifications_on_frame() # display circle that shows whether traffic light is red or not. if self.traffic_light_red: circle(self.frame, (25, 25), 25, (0, 0, 255), -1) else: circle(self.frame, (25, 25), 25, (0, 255, 0), -1) # Add tool toggle box and text in bottom left corner of screen rectangle(self.frame, (self.toggle_x1, self.toggle_y1), (self.toggle_x2, self.toggle_y2), (104, 82, 69), -2) if self.line_tool: putText(self.frame, 'I', (25, 700), FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, LINE_8, False) else: putText(self.frame, 'TL', (15, 700), FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, LINE_8, False) # Add save config box and text in bottom right corner of screen rectangle(self.frame, (self.config_x1, self.config_y1), (self.config_x2, self.config_y2), (104, 82, 69), -2) putText(self.frame, 'CF', (1230, 700), FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, LINE_8, False) # old grey box colour = (167, 170, 175) if len(self.line_pt) > 1: self.line_obj = line(self.frame, self.line_pt[0], self.line_pt[1], (0, 255, 0), 5) elif len(self.line_pt) == 0: # set line points to be points gathered from config print("Reading line point from config") self.line_pt = config.get_line() if len(self.rect_pt) > 1: self.rect_obj = rectangle(self.frame, self.rect_pt[0], self.rect_pt[1], (0, 0, 255), 2) elif len(self.rect_pt) == 0: # set line points to be points gathered from config print("Reading box point from config") self.rect_pt = config.get_box() imshow(self.ui_name, self.frame) if self.once: setMouseCallback(self.ui_name, self.click_and_crop) self.once = False
def face_detect(): face_cascade = cv2.CascadeClassifier( "/usr/share/opencv4/haarcascades/haarcascade_frontalface_default.xml" ) eye_cascade = cv2.CascadeClassifier( "/usr/share/opencv4/haarcascades/haarcascade_eye.xml" ) # ASSIGN CAMERA ADDRESS HERE camera_id="/dev/video0" # Full list of Video Capture APIs (video backends): https://docs.opencv.org/3.4/d4/d15/group__videoio__flags__base.html # For webcams, we use V4L2 video_capture = cv2.VideoCapture(camera_id, cv2.CAP_V4L2) """ # How to set video capture properties using V4L2: # Full list of Video Capture Properties for OpenCV: https://docs.opencv.org/3.4/d4/d15/group__videoio__flags__base.html #Select Pixel Format: # video_capture.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'YUYV')) # Two common formats, MJPG and H264 # video_capture.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPG')) # Default libopencv on the Jetson is not linked against libx264, so H.264 is not available # video_capture.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'H264')) # Select frame size, FPS: video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640) video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) video_capture.set(cv2.CAP_PROP_FPS, 30) """ if video_capture.isOpened(): try: cv2.namedWindow(window_title, cv2.WINDOW_AUTOSIZE) # Start counting the number of frames read and displayed # left_camera.start_counting_fps() while True: _, frame = video_capture.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) roi_gray = gray[y : y + h, x : x + w] roi_color = frame[y : y + h, x : x + w] eyes = eye_cascade.detectMultiScale(roi_gray) for (ex, ey, ew, eh) in eyes: cv2.rectangle( roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2 ) # Check to see if the user closed the window # Under GTK+, WND_PROP_VISIBLE does not work correctly. Under Qt it does # GTK - Substitute WND_PROP_AUTOSIZE to detect if window has been closed by user if cv2.getWindowProperty(window_title, cv2.WND_PROP_AUTOSIZE) >= 0: cv2.imshow(window_title, frame) else: break keyCode = cv2.waitKey(30) & 0xFF # Stop the program on the ESC key or 'q' if keyCode == 27 or keyCode == ord('q'): break finally: video_capture.release() cv2.destroyAllWindows() else: print("Unable to open camera")
if __name__ == "__main__": image_store = None color = np.array((0, 0, 0)) cv2.startWindowThread() cv2.namedWindow("ColorThresholding") cv2.setMouseCallback( "ColorThresholding", (lambda event, x, y, flags, user_data: on_mouse( event, x, y, flags, user_data, image_store, color))) cv2.createTrackbar("R", "ColorThresholding", 255, 255, on_scale) cv2.createTrackbar("G", "ColorThresholding", 255, 255, on_scale) cv2.createTrackbar("B", "ColorThresholding", 255, 255, on_scale) cam = cv2.VideoCapture(0) while cv2.getWindowProperty('ColorThresholding', 0) != -1: # raw image capture and preparation ret, frame = cam.read() if not ret: print("Could not get a frame from the camera, exiting.") break # pre-processing frame_pre_processed = pre_process_image(frame) # store image to select color via mouse-click image_store = frame_pre_processed # processing mask = select_range(frame_pre_processed, color) # post-processing post_processed_image = post_process_image(frame, frame_pre_processed, mask)
def depth_compute(self): msg_header = Header() msg_info = CameraInfo() msg_info.height = self.image_size msg_info.width = self.image_size msg_info.distortion_model = "plumb_bob" msg_info.K = [self.f, 0, self.c, 0, self.f, self.c, 0, 0, 1] msg_info.D = [0, 0, 0, 0, 0] msg_info.R = [1, 0, 0, 0, 1, 0, 0, 0, 1] msg_info.P = [self.f, 0, self.c, 0, 0, self.f, self.c, 0, 0, 0, 1, 0] if self.show_window: cv2.namedWindow("Depth Results", cv2.WINDOW_NORMAL) rate = rospy.Rate(self.freq) while not rospy.is_shutdown(): while not all(self.callback_ready): time.sleep(1e-3) t = time.time() self.code_ready = False self.center_undistorted = {"left" : cv2.remap(src = self.img_left, map1 = self.undistort_rectify["left"][0], map2 = self.undistort_rectify["left"][1], interpolation = cv2.INTER_LINEAR), "right": cv2.remap(src = self.img_right, map1 = self.undistort_rectify["right"][0], map2 = self.undistort_rectify["right"][1], interpolation = cv2.INTER_LINEAR)} self.disparity = self.stereo.compute(self.center_undistorted["left"], self.center_undistorted["right"]) / 16.0 self.depth = numpy.uint16(self.f * 1000.0*abs(self.T[0]) / (self.disparity[:,self.m:]+0.001)) self.depth[(self.depth > 10000) | (self.depth < 100)] = 0.0 self.color = cv2.cvtColor(self.center_undistorted["left"][:,self.m:], cv2.COLOR_GRAY2RGB) msg_depth = self.bridge.cv2_to_imgmsg(self.depth, encoding="passthrough") msg_color = self.bridge.cv2_to_imgmsg(self.color, encoding="rgb8") msg_header.seq += 1 msg_header.stamp = self.stamp msg_header.frame_id = self.prefix + "_aligned_depth_to_color_optical_frame" msg_depth.header = msg_header msg_color.header = msg_header msg_info.header = msg_header self.pub_depth_image.publish(msg_depth) self.pub_depth_info.publish(msg_info) self.pub_color_image.publish(msg_color) self.pub_color_info.publish(msg_info) if self.publish_tf: self.msg_tf = tf.TransformBroadcaster() self.msg_tf.sendTransform( (0.0, 0.0, 0.0), (0.0, 0.0, 0.0, 1.0), self.stamp, self.prefix + "_aligned_depth_to_color_optical_frame", self.child_frame_id) if self.show_window: img_window = numpy.hstack((self.color[:,:,0], numpy.uint8(255.0*self.depth/numpy.max(self.depth)))) cv2.imshow("Depth Results", img_window) cv2.waitKey(1) if cv2.getWindowProperty("Depth Results", cv2.WND_PROP_VISIBLE) < 1: self.mode = False self.callback_ready = [False, False, False, False] self.code_ready = True sys.stdout.write("0: %3dx%3d depth image computed in %.0fms\n" % (self.image_size, self.image_size, 1000*(time.time()-t))) sys.stdout.flush() rate.sleep()
def main(): global resize_output, resize_output_width, resize_output_height if (not handle_args()): print_usage() return 1 # configure the NCS mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2) # Get a list of ALL the sticks that are plugged in devices = mvnc.EnumerateDevices() if len(devices) == 0: print('No devices found') quit() # Pick the first stick to run the network device = mvnc.Device(devices[0]) # Open the NCS device.OpenDevice() graph_filename = 'graph' # Load graph file to memory buffer with open(graph_filename, mode='rb') as f: graph_data = f.read() # allocate the Graph instance from NCAPI by passing the memory buffer ssd_mobilenet_graph = device.AllocateGraph(graph_data) # get list of all the .mp4 files in the image directory input_video_filename_list = os.listdir(input_video_path) input_video_filename_list = [i for i in input_video_filename_list if i.endswith('.mp4')] if (len(input_video_filename_list) < 1): # no images to show print('No video (.mp4) files found') return 1 cv2.namedWindow(cv_window_name) cv2.moveWindow(cv_window_name, 10, 10) exit_app = False while (True): for input_video_file in input_video_filename_list : cap = cv2.VideoCapture(input_video_file) actual_frame_width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) actual_frame_height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) print ('actual video resolution: ' + str(actual_frame_width) + ' x ' + str(actual_frame_height)) if ((cap == None) or (not cap.isOpened())): print ('Could not open video device. Make sure file exists:') print ('file name:' + input_video_file) print ('Also, if you installed python opencv via pip or pip3 you') print ('need to uninstall it and install from source with -D WITH_V4L=ON') print ('Use the provided script: install-opencv-from_source.sh') exit_app = True break frame_count = 0 start_time = time.time() end_time = start_time while(True): ret, display_image = cap.read() if (not ret): end_time = time.time() print("No image from from video device, exiting") break # check if the window is visible, this means the user hasn't closed # the window via the X button prop_val = cv2.getWindowProperty(cv_window_name, cv2.WND_PROP_ASPECT_RATIO) if (prop_val < 0.0): end_time = time.time() exit_app = True break run_inference(display_image, ssd_mobilenet_graph) if (resize_output): display_image = cv2.resize(display_image, (resize_output_width, resize_output_height), cv2.INTER_LINEAR) cv2.imshow(cv_window_name, display_image) raw_key = cv2.waitKey(1) if (raw_key != -1): if (handle_keys(raw_key) == False): end_time = time.time() exit_app = True break frame_count += 1 frames_per_second = frame_count / (end_time - start_time) print('Frames per Second: ' + str(frames_per_second)) cap.release() if (exit_app): break; if (exit_app): break # Clean up the graph and the device ssd_mobilenet_graph.DeallocateGraph() device.CloseDevice() cv2.destroyAllWindows()
def openfichier(datacross,patient_path_complet_ref,slnroiref,slnroicomp,tabroiref,tabroicomp,tabscanLung,volroiref,volroicomp): global quitl,dimtabx,dimtaby,patchi,ix,iy print 'openfichier start' quitl=False num_class=len(classif) slnt=datacross[0] dimtabx=datacross[1] dimtaby=dimtabx # print slnroi patchi=False ix=0 iy=0 (top,tail)=os.path.split(patient_path_complet_ref) # pdirk = os.path.join(patient_path_complet_ref,source_name) pdirk = os.path.join(patient_path_complet_ref,sroi) list_image={} cdelimter='_' extensionimage='.'+typei1 limage=[name for name in os.listdir(pdirk) if name.find('.'+typei1,1)>0 ] lenlimage=len(slnroiref) # print len(limage), sln for iimage in limage: sln=rsliceNum(iimage,cdelimter,extensionimage) list_image[sln]=iimage # print sln image0=os.path.join(pdirk,list_image[slnroiref[0]]) img = cv2.imread(image0,1) img=cv2.resize(img,(dimtaby,dimtabx),interpolation=cv2.INTER_LINEAR) cv2.namedWindow('compare',cv2.WINDOW_NORMAL) cv2.namedWindow("Sliderfic",cv2.WINDOW_NORMAL) cv2.namedWindow("comparedata",cv2.WINDOW_AUTOSIZE) cv2.createTrackbar( 'Brightness','Sliderfic',0,100,nothing) cv2.createTrackbar( 'Contrast','Sliderfic',50,100,nothing) cv2.createTrackbar( 'Flip','Sliderfic',0,lenlimage-1,nothings) cv2.createTrackbar( 'All','Sliderfic',1,1,nothings) cv2.createTrackbar( 'None','Sliderfic',0,1,nothings) viewasked={} for key1 in usedclassif: # print key1 viewasked[key1]=True cv2.createTrackbar( key1,'Sliderfic',0,1,nothings) nbdig=0 numberentered={} initimg = np.zeros((dimtabx,dimtaby,3), np.uint8) slicenumberold=0 viewaskedold={} for keyne in usedclassif: viewaskedold[keyne]=False datav = np.zeros((500,900,3), np.uint8) imgtext = np.zeros((dimtabx,dimtaby,3), np.uint8) while(1): imgwip = np.zeros((200,200,3), np.uint8) cv2.setMouseCallback('compare',draw_circle,img) c = cv2.getTrackbarPos('Contrast','Sliderfic') l = cv2.getTrackbarPos('Brightness','Sliderfic') fld = cv2.getTrackbarPos('Flip','Sliderfic') allview = cv2.getTrackbarPos('All','Sliderfic') noneview = cv2.getTrackbarPos('None','Sliderfic') fl=slnroiref[fld] key = cv2.waitKey(1000) # if key != -1: # print key if key >47 and key<58: numberfinal=0 knum=key-48 # print 'this is number',knum numberentered[nbdig]=knum nbdig+=1 for i in range (nbdig): numberfinal=numberfinal+numberentered[i]*10**(nbdig-1-i) # print numberfinal numberfinal = min(slnt-1,numberfinal) if numberfinal>0: writeslice(numberfinal,initimg) if nbdig>0 and key ==8: numberfinal=0 nbdig=nbdig-1 for i in range (nbdig): numberfinal=numberfinal+numberentered[i]*10**(nbdig-1-i) # print numberfinal if numberfinal>0: writeslice(numberfinal,initimg) else: cv2.rectangle(initimg, (5,60), (150,50), black, -1) if nbdig>0 and key ==13 and numberfinal>0 : if numberfinal in slnroiref: # print numberfinal numberfinal = min(slnt-1,numberfinal) # writeslice(numberfinal,initimg) cv2.rectangle(initimg, (5,10), (150,20), black, -1) fld=slnroiref.index(numberfinal) # print fl,numberfinal cv2.setTrackbarPos('Flip','Sliderfic' ,fld) else: print 'number not in set' # cv2.rectangle(initimg, (5,60), (150,50), black, -1) cv2.rectangle(initimg, (5,10), (150,20), red, -1) cv2.putText(initimg,'NO ROI slice '+str(numberfinal)+'!',(5,20),cv2.FONT_HERSHEY_PLAIN,0.7,white,1 ) # cv2.putText(initimg, 'NO ROI on this slice',(5,60),cv2.FONT_HERSHEY_PLAIN,5,red,2,cv2.LINE_AA) # time.sleep(5) # fl=numberfinal numberfinal=0 nbdig=0 numberentered={} if key==2424832: fld=max(0,fld-1) cv2.setTrackbarPos('Flip','Sliderfic' ,fld) if key==2555904: fld=min(lenlimage-1,fld+1) cv2.setTrackbarPos('Flip','Sliderfic' ,fld) if allview==1: for key2 in usedclassif: cv2.setTrackbarPos(key2,'Sliderfic' ,1) cv2.setTrackbarPos('All','Sliderfic' ,0) if noneview==1: for key2 in usedclassif: cv2.setTrackbarPos(key2,'Sliderfic' ,0) cv2.setTrackbarPos('None','Sliderfic' ,0) for key2 in usedclassif: s = cv2.getTrackbarPos(key2,'Sliderfic') if s==1: viewasked[key2]=True else: viewasked[key2]=False slicenumber=fl # print slicenumber imagel=os.path.join(pdirk,list_image[slicenumber]) img = cv2.imread(imagel,1) img=cv2.resize(img,(dimtaby,dimtabx),interpolation=cv2.INTER_LINEAR) # print img.shape # print initimg.shape img=cv2.add(img,initimg) imglumi=lumi(img,l) imcontrast=contrasti(imglumi,c) imcontrast=cv2.cvtColor(imcontrast,cv2.COLOR_BGR2RGB) drawok=False if slicenumber != slicenumberold: slicenumberold=slicenumber drawok=True for keyne in usedclassif: if viewasked[keyne]!=viewaskedold[keyne]: viewaskedold[keyne]=viewasked[keyne] # print 'change' drawok=True if drawok: # print 'view' imgtext = np.zeros((dimtabx,dimtaby,3), np.uint8) cv2.putText(imgwip,'WIP',(10,10),cv2.FONT_HERSHEY_PLAIN,5,red,2,cv2.LINE_AA) cv2.imshow('wip',imgwip) imgn,datav= drawpatch(dimtabx,dimtaby,num_class,slicenumber,viewasked, slnroiref,slnroicomp,tabroiref,tabroicomp,tabscanLung,viewasked,volroiref,volroicomp) cv2.putText(datav,'slice number :'+str(slicenumber),(10,210),cv2.FONT_HERSHEY_PLAIN,0.7,white,1) cv2.putText(datav,'patient Name :'+tail,(10,220),cv2.FONT_HERSHEY_PLAIN,0.7,white,1) cv2.destroyWindow("wip") # imgngray = cv2.cvtColor(imgn,cv2.COLOR_BGR2GRAY) # np.putmask(imgngray,imgngray>0,255) # mask_inv = cv2.bitwise_not(imgngray) # outy=cv2.bitwise_and(imcontrast,imcontrast,mask=mask_inv) imgt=cv2.add(imcontrast,imgn) dxrect=(dimtaby/2) cv2.rectangle(imgt,(dxrect,dimtabx-30),(dxrect+20,dimtabx-10),red,-1) cv2.putText(imgt,'quit',(dxrect+10,dimtabx-10),cv2.FONT_HERSHEY_PLAIN,1,yellow,1,cv2.LINE_AA) imgtoshow=cv2.add(imgt,imgtext) imgtoshow=cv2.cvtColor(imgtoshow,cv2.COLOR_BGR2RGB) imsstatus=cv2.getWindowProperty('Sliderfic', 0) imistatus= cv2.getWindowProperty('compare', 0) imdstatus=cv2.getWindowProperty('comparedata', 0) # print imsstatus,imistatus,imdstatus if (imdstatus==0) and (imsstatus==0) and (imistatus==0) : cv2.imshow('compare',imgtoshow) cv2.imshow('comparedata',datav) else: quitl=True if quitl or cv2.waitKey(20) & 0xFF == 27 : break quitl=False cv2.destroyWindow("compare") cv2.destroyWindow("Sliderfic") cv2.destroyWindow("comparedata") return ''
def main(): global gn_mean, gn_labels, actual_frame_height, actual_frame_width, TY_BOX_PROBABILITY_THRESHOLD, TY_MAX_IOU print_info() # Set logging level and initialize/open the first NCS we find mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 0) devices = mvnc.EnumerateDevices() if len(devices) < 2: print('This application requires two NCS devices.') print('Insert two devices and try again!') return 1 ty_device = mvnc.Device(devices[0]) ty_device.OpenDevice() gn_device = mvnc.Device(devices[1]) gn_device.OpenDevice() #Load tiny yolo graph from disk and allocate graph via API try: with open(tiny_yolo_graph_file, mode='rb') as ty_file: ty_graph_from_disk = ty_file.read() ty_graph = ty_device.AllocateGraph(ty_graph_from_disk) except: print('Error - could not load tiny yolo graph file') ty_device.CloseDevice() gn_device.CloseDevice() return 1 #Load googlenet graph from disk and allocate graph via API try: with open(googlenet_graph_file, mode='rb') as gn_file: gn_graph_from_disk = gn_file.read() gn_graph = gn_device.AllocateGraph(gn_graph_from_disk) except: print('Error - could not load googlenet graph file') ty_device.CloseDevice() gn_device.CloseDevice() return 1 # GoogLenet initialization EXAMPLES_BASE_DIR = '../../' gn_mean = np.load(EXAMPLES_BASE_DIR + 'data/ilsvrc12/ilsvrc_2012_mean.npy').mean(1).mean( 1) # loading the mean file gn_labels_file = EXAMPLES_BASE_DIR + 'data/ilsvrc12/synset_words.txt' gn_labels = np.loadtxt(gn_labels_file, str, delimiter='\t') for label_index in range(0, len(gn_labels)): temp = gn_labels[label_index].split(',')[0].split(' ', 1)[1] gn_labels[label_index] = temp # get list of all the .jpg files in the image directory input_video_filename_list = os.listdir(input_video_path) input_video_filename_list = [ i for i in input_video_filename_list if i.endswith('.mp4') ] if (len(input_video_filename_list) < 1): # no images to show print('No .mp4 files found') return 1 exit_app = False print('Starting GUI, press Q to quit') cv2.namedWindow(cv_window_name1) cv2.namedWindow(cv_window_name2) cv2.namedWindow(cv_window_name3) cv2.waitKey(1) TY_MAX_IOU = 0.15 TY_BOX_PROBABILITY_THRESHOLD = 0.13 while (True): while (True): video_device1 = cv2.VideoCapture(3) #actual_frame_width1 = video_device1.get(cv2.CAP_PROP_FRAME_WIDTH) #actual_frame_height1 = video_device1.get(cv2.CAP_PROP_FRAME_HEIGHT) print('actual video resolution: ' + str(actual_frame_width) + ' x ' + str(actual_frame_height)) if ((video_device1 == None) or (not video_device1.isOpened())): print('Could not open video device1. Make sure file exists:') #print ('file name:' + input_video_file) print( 'Also, if you installed python opencv via pip or pip3 you') print( 'need to uninstall it and install from source with -D WITH_V4L=ON' ) print('Use the provided script: install-opencv-from_source.sh') video_device2 = cv2.VideoCapture(4) #actual_frame_width2 = video_device2.get(cv2.CAP_PROP_FRAME_WIDTH) #actual_frame_height2 = video_device2.get(cv2.CAP_PROP_FRAME_HEIGHT) print('actual video resolution: ' + str(actual_frame_width) + ' x ' + str(actual_frame_height)) if ((video_device2 == None) or (not video_device2.isOpened())): print('Could not open video device2. Make sure file exists:') #print ('file name:' + input_video_file) print( 'Also, if you installed python opencv via pip or pip3 you') print( 'need to uninstall it and install from source with -D WITH_V4L=ON' ) print('Use the provided script: install-opencv-from_source.sh') video_device3 = cv2.VideoCapture(5) #actual_frame_width3 = video_device3.get(cv2.CAP_PROP_FRAME_WIDTH) #actual_frame_height3 = video_device3.get(cv2.CAP_PROP_FRAME_HEIGHT) print('actual video resolution: ' + str(actual_frame_width) + ' x ' + str(actual_frame_height)) if ((video_device3 == None) or (not video_device3.isOpened())): print('Could not open video device3. Make sure file exists:') #print ('file name:' + input_video_file) print( 'Also, if you installed python opencv via pip or pip3 you') print( 'need to uninstall it and install from source with -D WITH_V4L=ON' ) print('Use the provided script: install-opencv-from_source.sh') frame_count = 0 start_time = time.time() while True: # Read image from video device, ret_val1, input_image1 = video_device1.read() if (not ret_val1): end_time = time.time() print("No image from from video device1, exiting") break ret_val2, input_image2 = video_device2.read() if (not ret_val2): end_time = time.time() print("No image from from video device2, exiting") break ret_val3, input_image3 = video_device3.read() if (not ret_val3): end_time = time.time() print("No image from from video device3, exiting") break # resize image to network width and height # then convert to float32, normalize (divide by 255), # and finally convert to float16 to pass to LoadTensor as input # for an inference input_image1 = cv2.resize( input_image1, (TY_NETWORK_IMAGE_WIDTH, TY_NETWORK_IMAGE_HEIGHT), cv2.INTER_CUBIC) #ret1=cv2.resize(input_image1,(300,100),cv2.INTER_LINEAR) #ret2=cv2.resize(input_image2,(300,100),cv2.INTER_LINEAR) #ret3=cv2.resize(input_image3,(300,100),cv2.INTER_LINEAR) #input_image[0:100,0:300]=ret1[0:100,0:300] #input_image[100:200,0:300]=ret2[0:100,0:300] #input_image[200:300,0:300]=ret3[0:100,0:300] # save a display image as read from video device. display_image1 = input_image1.copy() # modify input_image for TinyYolo input input_image1 = input_image1.astype(np.float32) input_image1 = np.divide(input_image1, 255.0) # Load tensor and get result. This executes the inference on the NCS ty_graph.LoadTensor(input_image1.astype(np.float16), 'user object') output, userobj = ty_graph.GetResult() # filter out all the objects/boxes that don't meet thresholds filtered_objs = filter_objects(output.astype(np.float32), input_image1.shape[1], input_image1.shape[0]) get_googlenet_classifications(gn_graph, display_image1, filtered_objs) # check if the window is visible, this means the user hasn't closed # the window via the X button prop_val = cv2.getWindowProperty(cv_window_name1, cv2.WND_PROP_ASPECT_RATIO) if (prop_val < 0.0): end_time = time.time() exit_app = True break overlay_on_image(display_image1, filtered_objs) # resize back to original size so image doesn't look squashed # It might be better to resize the boxes to match video dimensions # and overlay them directly on the video image returned from video device. display_image1 = cv2.resize( display_image1, (TY_NETWORK_IMAGE_WIDTH, TY_NETWORK_IMAGE_HEIGHT), cv2.INTER_CUBIC) input_image2 = cv2.resize(input_image2, (448, 448), cv2.INTER_CUBIC) #ret1=cv2.resize(input_image1,(300,100),cv2.INTER_LINEAR) #ret2=cv2.resize(input_image2,(300,100),cv2.INTER_LINEAR) #ret3=cv2.resize(input_image3,(300,100),cv2.INTER_LINEAR) #input_image[0:100,0:300]=ret1[0:100,0:300] #input_image[100:200,0:300]=ret2[0:100,0:300] #input_image[200:300,0:300]=ret3[0:100,0:300] # save a display image as read from video device. #display_image2 = input_image2.copy() # modify input_image for TinyYolo input #input_image2 = input_image2.astype(np.float32) #input_image2 = np.divide(input_image2, 255.0) # Load tensor and get result. This executes the inference on the NCS #ty_graph.LoadTensor(input_image2.astype(np.float16), 'user object') #output, userobj = ty_graph.GetResult() # filter out all the objects/boxes that don't meet thresholds #filtered_objs = filter_objects(output.astype(np.float32), input_image2.shape[1], input_image1.shape[0]) #get_googlenet_classifications(gn_graph, display_image2, filtered_objs) # check if the window is visible, this means the user hasn't closed # the window via the X button #prop_val = cv2.getWindowProperty(cv_window_name2, cv2.WND_PROP_ASPECT_RATIO) #if (prop_val < 0.0): #end_time = time.time() #exit_app = True #break #overlay_on_image(display_image2, filtered_objs) # resize back to original size so image doesn't look squashed # It might be better to resize the boxes to match video dimensions # and overlay them directly on the video image returned from video device. #display_image2 = cv2.resize(display_image2, (TY_NETWORK_IMAGE_WIDTH, TY_NETWORK_IMAGE_HEIGHT),cv2.INTER_CUBIC) input_image3 = cv2.resize(input_image3, (448, 448), cv2.INTER_CUBIC) #ret1=cv2.resize(input_image1,(300,100),cv2.INTER_LINEAR) #ret2=cv2.resize(input_image2,(300,100),cv2.INTER_LINEAR) #ret3=cv2.resize(input_image3,(300,100),cv2.INTER_LINEAR) #input_image[0:100,0:300]=ret1[0:100,0:300] #input_image[100:200,0:300]=ret2[0:100,0:300] #input_image[200:300,0:300]=ret3[0:100,0:300] # save a display image as read from video device. #display_image3 = input_image3.copy() # modify input_image for TinyYolo input #input_image3 = input_image3.astype(np.float32) #input_image3 = np.divide(input_image3, 255.0) # Load tensor and get result. This executes the inference on the NCS #ty_graph.LoadTensor(input_image3.astype(np.float16), 'user object') #output, userobj = ty_graph.GetResult() # filter out all the objects/boxes that don't meet thresholds #filtered_objs = filter_objects(output.astype(np.float32), input_image3.shape[1], input_image1.shape[0]) #get_googlenet_classifications(gn_graph, display_image3, filtered_objs) # check if the window is visible, this means the user hasn't closed # the window via the X button #prop_val = cv2.getWindowProperty(cv_window_name3, cv2.WND_PROP_ASPECT_RATIO) #if (prop_val < 0.0): #end_time = time.time() #exit_app = True #break #overlay_on_image(display_image3, filtered_objs) # resize back to original size so image doesn't look squashed # It might be better to resize the boxes to match video dimensions # and overlay them directly on the video image returned from video device. #display_image3 = cv2.resize(display_image3, (TY_NETWORK_IMAGE_WIDTH, TY_NETWORK_IMAGE_HEIGHT),cv2.INTER_CUBIC) # update the GUI window with new image cv2.imshow(cv_window_name1, display_image1) cv2.imshow(cv_window_name2, input_image2) cv2.imshow(cv_window_name3, input_image3) raw_key = cv2.waitKey(1) if (raw_key != -1): if (handle_keys(raw_key) == False): end_time = time.time() exit_app = True break frame_count = frame_count + 1 frames_per_second = frame_count / (end_time - start_time) print('Frames per Second: ' + str(frames_per_second)) # close video device video_device1.release() video_device2.release() video_device3.release() if (exit_app): break if (exit_app): break # clean up tiny yolo ty_graph.DeallocateGraph() ty_device.CloseDevice() # Clean up googlenet gn_graph.DeallocateGraph() gn_device.CloseDevice() print('Finished')
def draw(h, w, tah): canvas = np.ones((h + tah, w), dtype=np.float32) cv2.line(canvas, (0, tah - 1), (w, tah - 1), (0, 0, 0), 1) cv2.line(canvas, (201, 0), (201, h + tah), (.5, 255, 255), 1) cv2.line(canvas, (402, 0), (402, h + tah), (.5, 255, 255), 1) font = cv2.FONT_HERSHEY_PLAIN font2 = cv2.FONT_HERSHEY_TRIPLEX font3 = cv2.FONT_HERSHEY_SIMPLEX font4 = cv2.FONT_HERSHEY_DUPLEX #left box text1 = "Press 'C' to clear" cv2.putText(canvas, text1, (20, 10), font, 0.9, (0.5, 255, 0), 1) text2 = "Press 'E' to evaluate" cv2.putText(canvas, text2, (20, 22), font, 0.9, (0.5, 255, 0), 1) #middle box text3 = "Prediction" cv2.putText(canvas, text3, (240, 22), font3, .7, (0, 255, 0), 1) #right box text4 = "Confidence" cv2.putText(canvas, text4, (440, 22), font3, .7, (0, 255, 0), 1) img = canvas.copy() sketch = Sketcher(name, [img], lambda: ((0, 255, 0), 255)) while (True): if cv2.waitKey(0) == ord('c'): # clear the screen print("Clear") img[:] = canvas sketch.show() if cv2.waitKey(0) == ord('e'): # evaluate drawing # remove previous results from screen cv2.rectangle(img, (202, tah), (400, h + tah), (255, 255, 255), -1) cv2.rectangle(img, (404, tah), (600, h + tah), (255, 255, 255), -1) img2 = process( img) #call process img to apply filtering and edge detection #convert image into tensor input to_eval = tf.estimator.inputs.numpy_input_fn(x={"x": img2}, y=None, shuffle=False) # evaluate input image, Returns np.array output1 = classifier.predict(input_fn=to_eval) output2 = classifier.predict(input_fn=to_eval) #extract prediction class and probabilities from classfier output_classes = [c['classes'] for c in output1] output_prob = [p['probabilities'] for p in output2] #sort probabilities probs = output_prob[0] vals = [] for i in range(0, len(probs)): vals.append((i, probs[i])) sorted_probs = sorted(vals, key=lambda x: x[1]) #determine confidency level if sorted_probs[-1][1] >= 0.9: conf = 'Very Confident' elif sorted_probs[-1][1] < 0.9 and sorted_probs[-1][1] >= 0.8: conf = 'Confident' elif sorted_probs[-1][1] < 0.8 and sorted_probs[-1][1] >= 0.65: conf = 'Somewhat Confident' elif sorted_probs[-1][1] < 0.65: conf = 'Not Confident' # print evaluation confidence prob2 = str(sorted_probs[-2][0]) + ' = ' + str(sorted_probs[-2][1]) cv2.putText(img, conf, (430, 115), font3, .6, (0, 255, 0), 1) # print prediction output = str(output_classes) cv2.putText(img, str(output[1]), (250, 150), font4, 4, (0, 255, 0), 3) sketch.show() print("Eval") if (cv2.getWindowProperty(name, 0) == -1) or (cv2.waitKey() == 27): break cv2.destroyAllWindows()
GREEN = (0, 255, 0) RED = (0, 0, 255) YELL = (0, 255, 255) #Filters path haar_faces = cv2.CascadeClassifier( './filters/haarcascade_frontalface_default.xml') haar_eyes = cv2.CascadeClassifier('./filters/haarcascade_eye.xml') haar_mouth = cv2.CascadeClassifier('./filters/Mouth.xml') haar_nose = cv2.CascadeClassifier('./filters/Nose.xml') #config WebCam video_capture = cv2.VideoCapture(0) cv2.imshow('Video', np.empty((5, 5), dtype=float)) while cv2.getWindowProperty('Video', 0) >= 0: # Capture frame-by-frame ret, frame = video_capture.read() #frame = cv2.imread('faceswap/lol2.jpg') faces = apply_Haar_filter(frame, haar_faces, 1.3, 5, 30) for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x + w, y + h), BLUE, 2) #blue sub_img = frame[y:y + h, x:x + w, :] eyes = apply_Haar_filter(sub_img, haar_eyes, 1.3, 10, 10) for (x2, y2, w2, h2) in eyes: cv2.rectangle(frame, (x + x2, y + y2), (x + x2 + w2, y + y2 + h2), YELL, 2)
def detect_video(yolo, video_path, output_path=""): import cv2 vid = cv2.VideoCapture(0 if video_path == '0' else video_path) if not vid.isOpened(): raise IOError("Couldn't open webcam or video") # here we encode the video to MPEG-4 for better compatibility, you can use ffmpeg later # to convert it to x264 to reduce file size: # ffmpeg -i test.mp4 -vcodec libx264 -f mp4 test_264.mp4 # #video_FourCC = cv2.VideoWriter_fourcc(*'XVID') if video_path == '0' else int(vid.get(cv2.CAP_PROP_FOURCC)) video_FourCC = cv2.VideoWriter_fourcc(*'XVID') if video_path == '0' else cv2.VideoWriter_fourcc(*"mp4v") video_fps = vid.get(cv2.CAP_PROP_FPS) video_size = (int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)), int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))) isOutput = True if output_path != "" else False if isOutput: print("!!! TYPE:", type(output_path), type(video_FourCC), type(video_fps), type(video_size)) out = cv2.VideoWriter(output_path, video_FourCC, (5. if video_path == '0' else video_fps), video_size) accum_time = 0 curr_fps = 0 fps = "FPS: ??\nTIME: ??" prev_time = timer() while True: try: return_value, frame = vid.read() image = Image.fromarray(frame) image = yolo.detect_image(image) result = np.asarray(image) curr_time = timer() exec_time = curr_time - prev_time prev_time = curr_time accum_time = accum_time + exec_time curr_fps = curr_fps + 1 if accum_time > 1: accum_time = accum_time - 1 fps = "FPS: " + str(curr_fps) time_str = "TIME: " + "{:.2f}".format(exec_time) curr_fps = 0 cv2.putText(result, text=fps, org=(3, 15), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.50, color=(255, 0, 0), thickness=2) cv2.putText(result, text=time_str, org=(3, 30), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.50, color=(255, 0, 0), thickness=2) cv2.namedWindow("result", cv2.WINDOW_NORMAL) cv2.setWindowProperty("result", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_GUI_EXPANDED) cv2.imshow("result", result) if isOutput: out.write(result) if (cv2.waitKey(1) & 0xFF == ord('q')) | (cv2.getWindowProperty("result", cv2.WND_PROP_VISIBLE) < 1): break except AttributeError: vid.release() vid = cv2.VideoCapture(0 if video_path == '0' else video_path) if not vid.isOpened(): raise IOError("Couldn't open webcam or video") continue # Release everything if job is finished vid.release() if isOutput: out.release() cv2.destroyAllWindows()
def start_cameras(): left_camera = CSI_Camera() left_camera.create_gstreamer_pipeline( sensor_id=0, sensor_mode=SENSOR_MODE_1080, exposure_low=13000, exposure_high=13000, gain_low=1, gain_high=22.25, framerate=30, flip_method=2, display_height=DISPLAY_HEIGHT, display_width=DISPLAY_WIDTH, ) left_camera.open(left_camera.gstreamer_pipeline) left_camera.start() right_camera = CSI_Camera() right_camera.create_gstreamer_pipeline( sensor_id=1, sensor_mode=SENSOR_MODE_1080, exposure_low=13000, exposure_high=13000, gain_low=1, gain_high=22.25, framerate=30, flip_method=2, display_height=DISPLAY_HEIGHT, display_width=DISPLAY_WIDTH, ) right_camera.open(right_camera.gstreamer_pipeline) right_camera.start() cv2.namedWindow("CSI Cameras", cv2.WINDOW_AUTOSIZE) if (not left_camera.video_capture.isOpened() or not right_camera.video_capture.isOpened()): # Cameras did not open, or no camera attached print("Unable to open any cameras") # TODO: Proper Cleanup SystemExit(0) try: # Start counting the number of frames read and displayed left_camera.start_counting_fps() right_camera.start_counting_fps() while cv2.getWindowProperty("CSI Cameras", 0) >= 0: left_image = read_camera(left_camera, show_fps) right_image = read_camera(right_camera, show_fps) # We place both images side by side to show in the window camera_images = np.hstack((left_image, right_image)) cv2.imshow("CSI Cameras", camera_images) left_camera.frames_displayed += 1 right_camera.frames_displayed += 1 # This also acts as a frame limiter # Stop the program on the ESC key if (cv2.waitKey(1) & 0xFF) == 27: break finally: left_camera.stop() left_camera.release() right_camera.stop() right_camera.release() cv2.destroyAllWindows()
def run(self): if os.name == 'nt': cap = cv2.VideoCapture(self.camera, cv2.CAP_DSHOW) else: cap = cv2.VideoCapture(self.camera) # Trying to set resolution (1600 x 1200) if self.width is not None: cap.set(cv2.CAP_PROP_FRAME_WIDTH, self.width) if self.height is not None: cap.set(cv2.CAP_PROP_FRAME_HEIGHT, self.height) if self.debug: print(f'current width: {cap.get(cv2.CAP_PROP_FRAME_WIDTH)}') print(f'current height: {cap.get(cv2.CAP_PROP_FRAME_HEIGHT)}') while cap.isOpened(): # Capture frame-by-frame ret, frame = cap.read() if ret is True: # Convert to gray frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Invert colors (if needed) if self.invert: frame = cv2.bitwise_not(frame) # Display the resulting frame cv2.imshow(self.name, frame) # Figure out dimensions of the frame height, width = frame.shape[:2] # Analyze the frame results = [] results += pyzbar.decode((frame.tobytes(), width, height)) results += pylibdmtx.decode((frame.tobytes(), width, height), timeout=100) if len(results): # print(f'results: {results}') for result in results: output = result.data # Debugging if self.debug: _output = output.decode('utf-8') print(f'output: {_output}') if len(output) <= DEFAULT_ARRAY_LEN: # Copy barcode into array self._output[:len(output)] = output[:] # Null the rest of the array self._output[len(output):] = [0]*(DEFAULT_ARRAY_LEN-len(output)) # Record the time self._last_epoch.value = time.time() # Set new capture flag self._new.value = True else: raise OverflowError('Shared array is too small to represent this barcode!') # Capture key press command = cv2.waitKey(delay=20) # Parse command if command > 0: command = chr(command) if self.debug: print(f'command: {command}') # Save frame for the code recognition (Manual capture) if command in ('s', 'S'): cv2.imwrite('barcode.jpg', frame) # Close Video capture elif command in ('q', 'Q', '\x1b'): break # Capture Window close using 'X' button try: if cv2.getWindowProperty(self.name, cv2.WND_PROP_VISIBLE) < 1: break except cv2.error: break # Stop via explicit command if self._stop.value: break else: break # When everything is done, release the capture cap.release() cv2.destroyAllWindows()
def capture(self, record=False): devIndices = [] cap = [] writers = [] winNames = [] outlets = [] frameCounter = 1 for r in range(self.tableView.rowCount()): if self.tableView.item(r, 0).checkState() == QtCore.Qt.Checked: devIndices.append(r) if len(devIndices) == 0: return for dev in devIndices: cap_i = cv2.VideoCapture(dev) if not cap_i.isOpened(): continue cap.append(cap_i) setDevParameters(cap_i, self.settings['Camera' + str(dev)]) winName = 'Camera ' + str(dev) winNames.append(winName) cv2.namedWindow(winName) if record: fps = cap_i.get(cv2.CAP_PROP_FPS) width = cap_i.get(cv2.CAP_PROP_FRAME_WIDTH) height = cap_i.get(cv2.CAP_PROP_FRAME_HEIGHT) filename = os.path.join(self.dataFolder, 'Camera' + str(dev) + '.avi') fourcc = cv2.VideoWriter_fourcc(*'XVID') writer_i = cv2.VideoWriter(filename, fourcc, fps, (int(width), int(height))) writers.append(writer_i) outlets.append(createOutlet(dev, filename)) self.postProcFiles.append(filename) if record: self.state = 'Recording' else: self.state = 'Capturing' self.timer.start(500) try: ret = True while self.state != "Stop" and ret: for i, cap_i in enumerate(cap): win_i = winNames[i] if cv2.getWindowProperty(win_i, cv2.WND_PROP_VISIBLE): ret, frame = cap_i.read() cv2.imshow(win_i, frame) if record: outlets[i].push_sample([frameCounter]) writers[i].write(frame) else: ret = False frameCounter += 1 cv2.waitKey(1) finally: for cap_i in cap: cap_i.release() cv2.destroyAllWindows() self.state = '' self.timer.stop() self.statusbar.showMessage(self.state)
textAt(330,580, str(round(sw.var.get("Depth"),2)), RED ) #if sliders need to be reZeroed (for if zeroing the thrusters came undone) if reZeroSliders: zeroSliders() reZeroSliders = False if Pause.change: Pause.change = False if Pause.pressed == True: #zero out dials Roll.change = True Roll.desiredBearing = -math.pi/2 writeHub(Roll) Pitch.change = True Pitch.desiredBearing = -math.pi/2 writeHub(Pitch) Yaw.change = True Yaw.desiredBearing = Yaw.actualBearing writeHub(Yaw) #zero out other sliders zeroSliders() reZeroSliders = True #if the k key is hit if k == 107 or k == 27 or cv2.getWindowProperty('Control Panel',1) < 1: sw.close() cv2.destroyWindow('Control Panel') break
def toggle_view(view_name, image): if views[view_name]: cv2.imshow(view_name, image) elif cv2.getWindowProperty(view_name, cv2.CV_WINDOW_AUTOSIZE) > 0: # get any window prop to check for existence cv2.destroyWindow(view_name)
print("FPS: %f fps / t = %f" % (100 / (t1 - t0), epoch * dt)) t0 = t1 #u_img = make_effect(u, display_scaling_factor) #print(u.min(), u.max()) cv2.imshow('u', (1-u)/2.) key = cv2.waitKey(1) & 0xFF if(key == ord('c')): c = (random.randint(0, N-1), random.randint(0, N-1)) model.erase_reactant(c , N/8) elif(key == ord('m')): mask = 0.75 + 0.25*np.random.random((N, N)) model.mask_reactant(mask) elif key == ord('s'): run = not run print("Running ? : " + str(run)) elif key == ord('i'): model.init() elif key == ord('p'): print("Saving u-%05d.png" % frame_id) cv2.imwrite("u-%05d.png" % frame_id, (np.minimum(255*u_img, 255)).astype(np.uint8)) frame_id += 1 elif key == ord('f'): screenmode = cv2.getWindowProperty("u", cv2.WND_PROP_FULLSCREEN) if(screenmode == normal_flag): cv2.setWindowProperty("u", cv2.WND_PROP_FULLSCREEN, fullscreen_flag) else: cv2.setWindowProperty("u", cv2.WND_PROP_FULLSCREEN, normal_flag)
t2.LoadTlsNodeCertificate() except: print "warning: could not load TLS certificate" t2.EnableNodeAnnounce() #Register the service type and the service RRN.RegisterServiceType(create_servicedef) RRN.RegisterServiceType(webcam_servicedef) RRN.RegisterService("Create","experimental.create.Create",obj) RRN.RegisterService("Webcam","experimental.createwebcam.WebcamHost",obj2) start_time=time.time() while True: img=create.get_image() if cv2.getWindowProperty('Simple Create Simulation',0) < 0: break cv2.imshow('Simple Create Simulation', img) if cv2.waitKey(25) != -1: break cv2.destroyAllWindows() create.stop_sim() RRN.Shutdown()
def main(): global gn_input_queue, gn_output_queue, ty_proc, gn_proc print_info() # Set logging level and initialize/open the first NCS we find mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 0) devices = mvnc.EnumerateDevices() if len(devices) < 2: print('This application requires two NCS devices.') print('Insert two devices and try again!') return 1 ty_device = mvnc.Device(devices[0]) ty_device.OpenDevice() gn_device = mvnc.Device(devices[1]) gn_device.OpenDevice() gn_proc = googlenet_processor(GOOGLENET_GRAPH_FILE, gn_device, gn_input_queue, gn_output_queue, QUEUE_WAIT_MAX, QUEUE_WAIT_MAX) print('Starting GUI, press Q to quit') cv2.namedWindow(cv_window_name) cv2.waitKey(1) frame_count = 0 start_time = time.time() end_time = start_time # Queue of camera images. Only need two spots camera_queue = queue.Queue(CAMERA_QUEUE_SIZE) # camera processor that will put camera images on the camera_queue camera_proc = camera_processor(camera_queue, CAMERA_QUEUE_PUT_WAIT_MAX, CAMERA_INDEX, CAMERA_REQUEST_VID_WIDTH, CAMERA_REQUEST_VID_HEIGHT, CAMERA_QUEUE_FULL_SLEEP_SECONDS) actual_camera_width = camera_proc.get_actual_camera_width() actual_camera_height = camera_proc.get_actual_camera_height() ty_output_queue = queue.Queue(TY_OUTPUT_QUEUE_SIZE) ty_proc = tiny_yolo_processor(TINY_YOLO_GRAPH_FILE, ty_device, camera_queue, ty_output_queue, TY_INITIAL_BOX_PROBABILITY_THRESHOLD, TY_INITIAL_MAX_IOU, QUEUE_WAIT_MAX, QUEUE_WAIT_MAX) gn_proc.start_processing() camera_proc.start_processing() ty_proc.start_processing() while True : (display_image, filtered_objs) = ty_output_queue.get(True, QUEUE_WAIT_MAX) get_googlenet_classifications(display_image, filtered_objs) #get_googlenet_classifications_no_queue(gn_proc, display_image, filtered_objs) # check if the window is visible, this means the user hasn't closed # the window via the X button prop_val = cv2.getWindowProperty(cv_window_name, cv2.WND_PROP_ASPECT_RATIO) if (prop_val < 0.0): end_time = time.time() ty_output_queue.task_done() break overlay_on_image(display_image, filtered_objs) # update the GUI window with new image cv2.imshow(cv_window_name, display_image) raw_key = cv2.waitKey(1) if (raw_key != -1): if (handle_keys(raw_key) == False): end_time = time.time() ty_output_queue.task_done() break frame_count = frame_count + 1 ty_output_queue.task_done() frames_per_second = frame_count / (end_time - start_time) print ('Frames per Second: ' + str(frames_per_second)) camera_proc.stop_processing() ty_proc.stop_processing() gn_proc.stop_processing() camera_proc.cleanup() # clean up tiny yolo ty_proc.cleanup() ty_device.CloseDevice() # Clean up googlenet gn_proc.cleanup() gn_device.CloseDevice() print('Finished')
def main(): global gn_mean, gn_labels, actual_camera_height, actual_camera_width print_info() # Set logging level and initialize/open the first NCS we find mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 0) devices = mvnc.EnumerateDevices() if len(devices) < 2: print('This application requires two NCS devices.') print('Insert two devices and try again!') return 1 ty_device = mvnc.Device(devices[0]) ty_device.OpenDevice() gn_device = mvnc.Device(devices[1]) gn_device.OpenDevice() #Load tiny yolo graph from disk and allocate graph via API try: with open(tiny_yolo_graph_file, mode='rb') as ty_file: ty_graph_from_disk = ty_file.read() ty_graph = ty_device.AllocateGraph(ty_graph_from_disk) except: print ('Error - could not load tiny yolo graph file') ty_device.CloseDevice() gn_device.CloseDevice() return 1 #Load googlenet graph from disk and allocate graph via API try: with open(googlenet_graph_file, mode='rb') as gn_file: gn_graph_from_disk = gn_file.read() gn_graph = gn_device.AllocateGraph(gn_graph_from_disk) except: print ('Error - could not load googlenet graph file') ty_device.CloseDevice() gn_device.CloseDevice() return 1 # GoogLenet initialization EXAMPLES_BASE_DIR = '../../' gn_mean = np.load(EXAMPLES_BASE_DIR + 'data/ilsvrc12/ilsvrc_2012_mean.npy').mean(1).mean(1) # loading the mean file gn_labels_file = EXAMPLES_BASE_DIR + 'data/ilsvrc12/synset_words.txt' gn_labels = np.loadtxt(gn_labels_file, str, delimiter='\t') for label_index in range(0, len(gn_labels)): temp = gn_labels[label_index].split(',')[0].split(' ', 1)[1] gn_labels[label_index] = temp print('Starting GUI, press Q to quit') cv2.namedWindow(cv_window_name) cv2.waitKey(1) camera_device = cv2.VideoCapture(CAMERA_INDEX) camera_device.set(cv2.CAP_PROP_FRAME_WIDTH, REQUEST_CAMERA_WIDTH) camera_device.set(cv2.CAP_PROP_FRAME_HEIGHT, REQUEST_CAMERA_HEIGHT) actual_camera_width = camera_device.get(cv2.CAP_PROP_FRAME_WIDTH) actual_camera_height = camera_device.get(cv2.CAP_PROP_FRAME_HEIGHT) print ('actual camera resolution: ' + str(actual_camera_width) + ' x ' + str(actual_camera_height)) if ((camera_device == None) or (not camera_device.isOpened())): print ('Could not open camera. Make sure it is plugged in.') print ('Also, if you installed python opencv via pip or pip3 you') print ('need to uninstall it and install from source with -D WITH_V4L=ON') print ('Use the provided script: install-opencv-from_source.sh') frame_count = 0 start_time = time.time() end_time = time.time() while True : # Read image from camera, ret_val, input_image = camera_device.read() if (not ret_val): print("No image from camera, exiting") break # resize image to network width and height # then convert to float32, normalize (divide by 255), # and finally convert to float16 to pass to LoadTensor as input # for an inference input_image = cv2.resize(input_image, (TY_NETWORK_IMAGE_WIDTH, TY_NETWORK_IMAGE_HEIGHT), cv2.INTER_LINEAR) # save a display image as read from camera. display_image = input_image.copy() # modify input_image for TinyYolo input input_image = input_image.astype(np.float32) input_image = np.divide(input_image, 255.0) input_image = input_image[:, :, ::-1] # convert to RGB # Load tensor and get result. This executes the inference on the NCS ty_graph.LoadTensor(input_image.astype(np.float16), 'user object') output, userobj = ty_graph.GetResult() # filter out all the objects/boxes that don't meet thresholds filtered_objs = filter_objects(output.astype(np.float32), input_image.shape[1], input_image.shape[0]) get_googlenet_classifications(gn_graph, display_image, filtered_objs) # check if the window is visible, this means the user hasn't closed # the window via the X button prop_val = cv2.getWindowProperty(cv_window_name, cv2.WND_PROP_ASPECT_RATIO) if (prop_val < 0.0): end_time = time.time() break overlay_on_image(display_image, filtered_objs) # resize back to original camera size so image doesn't look squashed # It might be better to resize the boxes to match camera dimensions # and overlay them directly on the camera size image. display_image = cv2.resize(display_image, (int(actual_camera_width), int(actual_camera_height)), cv2.INTER_LINEAR) # update the GUI window with new image cv2.imshow(cv_window_name, display_image) raw_key = cv2.waitKey(1) if (raw_key != -1): if (handle_keys(raw_key) == False): end_time = time.time() break frame_count = frame_count + 1 frames_per_second = frame_count / (end_time - start_time) print ('Frames per Second: ' + str(frames_per_second)) # close camera camera_device.release() # clean up tiny yolo ty_graph.DeallocateGraph() ty_device.CloseDevice() # Clean up googlenet gn_graph.DeallocateGraph() gn_device.CloseDevice() print('Finished')
if im is None: break im = imutils.resize(im[y1:y2, x1:x2], width=600) raw = im.copy() start = time() result = det.detect(im)['frame'] if det.process: text = '{:.4f}'.format((time()-start)/2) cv2.putText(result, text, (10, 50), cv2.FONT_HERSHEY_COMPLEX, 1.2, (0, 0, 255), 4) if size is None: size = (result.shape[1], result.shape[0]) fourcc = cv2.VideoWriter_fourcc( 'm', 'p', '4', 'v') # opencv3.0 videoWriter = cv2.VideoWriter( 'result_car.mp4', fourcc, fps, size) videoWriter.write(result) cv2.imshow('a', result) if cv2.waitKey(1) == 27: break if cv2.getWindowProperty('a', cv2.WND_PROP_AUTOSIZE) < 1: # 点x退出 break cap.release() videoWriter.release() cv2.destroyAllWindows()
def cv_display_source(videosrc, width, height, title, istype, **kwargs): """Displays a cv2.VideoCapture to a window. Args: videosrc: A source. Can be an image (imread) or video (VideoCapture). width: Width of video. 0 to automatically set width. height: Height of video. 0 to automatically set height. title: Title of window. istype: A SourceType representing whether the feed from the source is a video or a live feed. kwarg - keyexit: Value ID of keyboard key. If this value is True then the default key will be 'q'. Returns: A feed from the source in a new window. Note: If threading with display source, please use different source objects as inputs. """ keyexit = kwargs.get('keyexit', True) if isinstance(videosrc, cv2.VideoCapture) is not True and istype is not 2: raise ("Error, object provided is not cv2.VideoCapture object") elif isinstance(videosrc, cv2.VideoCapture) is True and istype is 2: raise ("Error, expected image as source.") if isinstance(videosrc, cv2.VideoCapture) is True: if width is 0: width = int(videosrc.get(3)) if height is 0: height = int(videosrc.get(4)) elif isinstance(videosrc, np.ndarray) is True: if width is 0: width = int(videosrc.shape[1]) if height is 0: height = int(videosrc.shape[0]) if (keyexit is True) or (keyexit is False and istype is 2): keyexit = 'q' if istype is 0: fps = 10 elif istype is 1: fps = int(videosrc.get(cv2.CAP_PROP_FPS)) * 10 windowName = "cv2WindowDisplay" cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) cv2.resizeWindow(windowName, width, height) cv2.setWindowTitle(windowName, title) if istype is not 2: while videosrc.isOpened(): if cv2.getWindowProperty(windowName, 0) < 0: videosrc.release() ret, frame = videosrc.read() cv2.imshow(windowName, frame) if keyexit is not False: if cv2.waitKey(100) & 0xFF is ord(keyexit): cv2.destroyWindow(windowName) videosrc.release() cv2.waitKey(fps) elif istype is 2 and isinstance(videosrc, np.ndarray) is True: cv2.imshow(windowName, videosrc) if cv2.waitKey(0) & 0xFF is ord(keyexit): cv2.destroyWindow(windowName)
"[q] to quit;\n" "[a] or [d] to change Image;\n" "[w] or [s] to change Class.\n" "%s" % img_path) # show edges key listener elif pressed_key == ord('e'): if edges_on == True: edges_on = False if WITH_QT: cv2.displayOverlay(WINDOW_NAME, "Edges turned OFF!", 1000) else: print("Edges turned OFF!") else: edges_on = True if WITH_QT: cv2.displayOverlay(WINDOW_NAME, "Edges turned ON!", 1000) else: print("Edges turned ON!") # quit key listener elif pressed_key == ord('q'): break """ Key Listeners END """ if WITH_QT: # if window gets closed then quit if cv2.getWindowProperty(WINDOW_NAME,cv2.WND_PROP_VISIBLE) < 1: break cv2.destroyAllWindows()
def video(): global coordinates coordinates = [] lowerBound = np.array([33, 80, 40]) higherBound = np.array([102, 255, 255]) cam = cv2.VideoCapture(0) # Defining kernel sizes for smoothing the image kernelOpen = np.ones((5, 5)) kernelClose = np.ones((20, 20)) timeMes = 0 # font for text # font = cv2.InitFont(cv2.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 1, 1) keyPressed = [] keysCo = {} recKeys = [] while True: ret, img = cam.read() cv2.flip(img, 1) img = cv2.resize(img, (1260, 700)) img = cv2.flip(img, 1) # converting color from Red, Blue, Green to Hue, Saturation and Value imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # finding mask using given image and color parameters mask = cv2.inRange(imgHSV, lowerBound, higherBound) # smoothing image maskOpen = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernelOpen) maskClose = cv2.morphologyEx(maskOpen, cv2.MORPH_CLOSE, kernelClose) # final mask after, smoothing, refilling maskFinal = maskClose conts, heirarchy = cv2.findContours(maskFinal.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[1:3] cs = [] cv2.drawContours(img, conts, -1, (255, 0, 0), 2) for i in range(0, 21): cv2.rectangle(img, (i * 60, 500), (i * 60 + 60, 700), (255, 255, 255), thickness=cv2.FILLED) cv2.rectangle(img, (i * 60, 500), (i * 60 + 60, 700), (0, 0, 0), 1) # cv2.rectangle(img, (45, 500), (75, 650), (0, 0, 0), thickness=cv2.FILLED) cv2.rectangle(img, (105, 500), (135, 650), (0, 0, 0), thickness=cv2.FILLED) # cv2.rectangle(img, (225, 500), (255, 650), (0, 0, 0), thickness=cv2.FILLED) cv2.rectangle(img, (285, 500), (315, 650), (0, 0, 0), thickness=cv2.FILLED) cv2.rectangle(img, (345, 500), (375, 650), (0, 0, 0), thickness=cv2.FILLED) # cv2.rectangle(img, (465, 500), (495, 650), (0, 0, 0), thickness=cv2.FILLED) cv2.rectangle(img, (525, 500), (555, 650), (0, 0, 0), thickness=cv2.FILLED) # cv2.rectangle(img, (645, 500), (675, 650), (0, 0, 0), thickness=cv2.FILLED) cv2.rectangle(img, (705, 500), (735, 650), (0, 0, 0), thickness=cv2.FILLED) cv2.rectangle(img, (765, 500), (795, 650), (0, 0, 0), thickness=cv2.FILLED) # cv2.rectangle(img, (885, 500), (915, 650), (0, 0, 0), thickness=cv2.FILLED) cv2.rectangle(img, (945, 500), (975, 650), (0, 0, 0), thickness=cv2.FILLED) # cv2.rectangle(img, (1065, 500), (1095, 650), (0, 0, 0), thickness=cv2.FILLED) cv2.rectangle(img, (1125, 500), (1155, 650), (0, 0, 0), thickness=cv2.FILLED) cv2.rectangle(img, (1185, 500), (1215, 650), (0, 0, 0), thickness=cv2.FILLED) # cv2.rectangle(img, (20, 20), (60, 45), (0, 0, 0), thickness=cv2.FILLED) keys = { 1: "C.wav", 2: "C_s.wav", 3: "D.wav", 4: "D_s.wav", 5: "E.wav", 6: "F.wav", 7: "F_s.wav", 8: "G.wav", 9: "G_s.wav", 10: "A.wav", 11: "A_s.wav", 12: "B.wav", 13: "C1.wav", 14: "C_s1.wav", 15: "D1.wav", 16: "D_s1.wav", 17: "E1.wav", 18: "F1.wav", 19: "F_s1.wav", 20: "G.wav", 21: "G_s.wav", 22: "A1.wav", 23: "A_s1.wav", 24: "B.wav", 25: "C2.wav", 26: "C_s2.wav", 27: "D2.wav", 28: "D_s2.wav", 29: "E2.wav", 30: "F2.wav", 31: "F_s2.wav", 32: "G2.wav", 33: "G_s2.wav", 34: "A2.wav", 35: "A_s2.wav", 36: "B2.wav" } keyID = 0 displayBlocks(img, coordinates, keysCo, keys) for i in range(0, len(conts)): x1, y1, w1, h1 = cv2.boundingRect(conts[i]) cx1 = int(x1 + w1 / 2) cy1 = int(y1 + h1 / 2) if 20 <= cx1 <= 60 and 20 <= cy1 <= 40: print("ligma") example.start_audio_recording() if 0 <= cx1 <= 45 and 500 <= cy1: keyID = 1 _thread.start_new_thread(play.play_audio, ( keyID, keys, )) cv2.rectangle(img, (0, 500), (60, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([0, 60])) recKeys.append(keyID) elif 45 <= cx1 <= 75 and 500 <= cy1: keyID = 2 _thread.start_new_thread(play.play_audio, ( keyID, keys, )) cv2.rectangle(img, (45, 500), (75, 650), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([45, 75])) recKeys.append(keyID) elif 75 <= cx1 <= 105 and 500 <= cy1: keyID = 3 _thread.start_new_thread(play.play_audio, ( keyID, keys, )) cv2.rectangle(img, (60, 500), (120, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([60, 120])) recKeys.append(keyID) elif 105 <= cx1 <= 135 and 500 <= cy1: keyID = 4 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (105, 500), (135, 650), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([105, 135])) recKeys.append(keyID) elif 135 <= cx1 <= 180 and 500 <= cy1: keyID = 5 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (120, 500), (180, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([120, 180])) recKeys.append(keyID) elif 180 <= cx1 <= 225 and 500 <= cy1: keyID = 6 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (180, 500), (240, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([180, 240])) recKeys.append(keyID) elif 225 <= cx1 <= 255 and 500 <= cy1: keyID = 7 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (225, 500), (255, 650), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([225, 255])) recKeys.append(keyID) elif 255 <= cx1 <= 285 and 500 <= cy1: keyID = 8 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (240, 500), (300, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([240, 300])) recKeys.append(keyID) elif 285 <= cx1 <= 315 and 500 <= cy1: keyID = 9 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (285, 500), (315, 650), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([285, 315])) recKeys.append(keyID) elif 315 <= cx1 <= 345 and 500 <= cy1: keyID = 10 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (300, 500), (360, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([300, 360])) recKeys.append(keyID) elif 345 <= cx1 <= 375 and 500 <= cy1: keyID = 11 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (345, 500), (375, 650), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([345, 375])) recKeys.append(keyID) elif 375 <= cx1 <= 420 and 500 <= cy1: keyID = 12 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (360, 500), (420, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([360, 420])) recKeys.append(keyID) elif 420 <= cx1 <= 465 and 500 <= cy1: keyID = 13 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (420, 500), (480, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([420, 480])) recKeys.append(keyID) elif 465 <= cx1 <= 495 and 500 <= cy1: keyID = 14 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (465, 500), (495, 650), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([465, 495])) recKeys.append(keyID) elif 495 <= cx1 <= 525 and 500 <= cy1: keyID = 15 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (480, 500), (540, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([480, 540])) recKeys.append(keyID) elif 525 <= cx1 <= 555 and 500 <= cy1: keyID = 16 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (525, 500), (555, 650), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([525, 555])) recKeys.append(keyID) elif 555 <= cx1 <= 600 and 500 <= cy1: keyID = 17 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (540, 500), (600, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([540, 600])) recKeys.append(keyID) elif 600 <= cx1 <= 645 and 500 <= cy1: keyID = 18 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (600, 500), (660, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([600, 660])) recKeys.append(keyID) elif 645 <= cx1 <= 675 and 500 <= cy1: keyID = 19 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (645, 500), (675, 650), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([645, 675])) recKeys.append(keyID) elif 675 <= cx1 <= 705 and 500 <= cy1: keyID = 20 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (660, 500), (720, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([660, 720])) recKeys.append(keyID) elif 705 <= cx1 <= 735 and 500 <= cy1: keyID = 21 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (705, 500), (735, 645), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([705, 735])) recKeys.append(keyID) elif 735 <= cx1 <= 765 and 500 <= cy1: keyID = 22 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (720, 500), (780, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([720, 780])) recKeys.append(keyID) elif 765 <= cx1 <= 795 and 500 <= cy1: keyID = 23 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (765, 500), (795, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([765, 795])) recKeys.append(keyID) elif 795 <= cx1 <= 840 and 500 <= cy1: keyID = 24 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (780, 500), (840, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([780, 840])) recKeys.append(keyID) elif 840 <= cx1 <= 885 and 500 <= cy1: keyID = 25 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (840, 500), (900, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([840, 900])) recKeys.append(keyID) elif 885 <= cx1 <= 915 and 500 <= cy1: keyID = 26 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (885, 500), (915, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([885, 915])) recKeys.append(keyID) elif 915 <= cx1 <= 945 and 500 <= cy1: keyID = 27 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (900, 500), (960, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([900, 960])) recKeys.append(keyID) elif 945 <= cx1 <= 975 and 500 <= cy1: keyID = 28 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (945, 500), (975, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([945, 975])) recKeys.append(keyID) elif 975 <= cx1 <= 1020 and 500 <= cy1: keyID = 29 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (960, 500), (1020, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([960, 1020])) recKeys.append(keyID) elif 1020 <= cx1 <= 1065 and 500 <= cy1: keyID = 30 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (1020, 500), (1080, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([1020, 1080])) recKeys.append(keyID) elif 1065 <= cx1 <= 1095 and 500 <= cy1: keyID = 31 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (1065, 500), (1095, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([1065, 1095])) recKeys.append(keyID) elif 1095 <= cx1 <= 1125 and 500 <= cy1: keyID = 32 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (1080, 500), (1140, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([1080, 1140])) recKeys.append(keyID) elif 1125 <= cx1 <= 1155 and 500 <= cy1: keyID = 33 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (1125, 500), (1155, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([1125, 1155])) recKeys.append(keyID) elif 1155 <= cx1 <= 1185 and 500 <= cy1: keyID = 34 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (1140, 500), (1200, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([1140, 1200])) recKeys.append(keyID) elif 1185 <= cx1 <= 1215 and 500 <= cy1: keyID = 35 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (1185, 500), (1215, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([1185, 1215])) recKeys.append(keyID) elif 1215 <= cx1 <= 1260 and 500 <= cy1: keyID = 36 _thread.start_new_thread(play.play_audio, (keyID, keys)) cv2.rectangle(img, (1200, 500), (1260, 700), (0, 0, 255), 2) keyPressed.append(keyID) keysCo[keyID] = keysCo.get(keyID, 1) + 1 coordinates.append(tuple([1200, 1260])) recKeys.append(keyID) cv2.imshow('Cam', img) if cv2.getWindowProperty('Cam', cv2.WND_PROP_VISIBLE) < 1: break cv2.waitKey(10) cv2.destroyAllWindows()
or key == ord("4") or key == ord("5")): cont = 0 if key == ord("1"): cont = 1 elif key == ord("2"): cont = 2 elif key == ord("3"): cont = 3 elif key == ord("4"): cont = 4 elif key == ord("5"): cont = 5 elif key == ord("c"): selection = 0 detectObject = 0 if (len(globalBoxes) > 0) and (0 < cont <= len(globalBoxes)): objectCoordinates = globalBoxes[cont - 1] selection = 0 else: print("Object does not comply with grasp rules... ") print("Scanning ... ") print("Press 's' key for selection mode ... ") if key in (27, ord("q")) or cv2.getWindowProperty( state.WIN_NAME, cv2.WND_PROP_AUTOSIZE) < 0: break # Stop streaming plt.show() pipeline.stop()
def run_camera(valid_output, validated_image_filename, graph): camera_device = cv2.VideoCapture(CAMERA_INDEX) camera_device.set(cv2.CAP_PROP_FRAME_WIDTH, REQUEST_CAMERA_WIDTH) camera_device.set(cv2.CAP_PROP_FRAME_HEIGHT, REQUEST_CAMERA_HEIGHT) actual_camera_width = camera_device.get(cv2.CAP_PROP_FRAME_WIDTH) actual_camera_height = camera_device.get(cv2.CAP_PROP_FRAME_HEIGHT) print ('actual camera resolution: ' + str(actual_camera_width) + ' x ' + str(actual_camera_height)) if ((camera_device == None) or (not camera_device.isOpened())): print ('Could not open camera. Make sure it is plugged in.') print ('Also, if you installed python opencv via pip or pip3 you') print ('need to uninstall it and install from source with -D WITH_V4L=ON') print ('Use the provided script: install-opencv-from_source.sh') return frame_count = 0 cv2.namedWindow(CV_WINDOW_NAME) found_match = False while True : # Read image from camera, ret_val, vid_image = camera_device.read() if (not ret_val): print("No image from camera, exiting") break frame_count += 1 frame_name = 'camera frame ' + str(frame_count) # run a single inference on the image and overwrite the # boxes and labels test_output = run_inference(vid_image, graph) if (face_match(valid_output, test_output)): print('PASS! File ' + frame_name + ' matches ' + validated_image_filename) found_match = True else: found_match = False print('FAIL! File ' + frame_name + ' does not match ' + validated_image_filename) overlay_on_image(vid_image, frame_name, found_match) # check if the window is visible, this means the user hasn't closed # the window via the X button prop_val = cv2.getWindowProperty(CV_WINDOW_NAME, cv2.WND_PROP_ASPECT_RATIO) if (prop_val < 0.0): print('window closed') break # display the results and wait for user to hit a key cv2.imshow(CV_WINDOW_NAME, vid_image) raw_key = cv2.waitKey(1) if (raw_key != -1): if (handle_keys(raw_key) == False): print('user pressed Q') break if (found_match): cv2.imshow(CV_WINDOW_NAME, vid_image) cv2.waitKey(0)
def main(): global gn_mean, gn_labels, input_image_filename_list print('Running NCS birds example') # get list of all the .jpg files in the image directory input_image_filename_list = os.listdir(input_image_path) input_image_filename_list = [input_image_path + '/' + i for i in input_image_filename_list if i.endswith('.jpg')] if (len(input_image_filename_list) < 1): # no images to show print('No .jpg files found') return 1 # Set logging level and initialize/open the first NCS we find mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 0) devices = mvnc.EnumerateDevices() if len(devices) < 2: print('This application requires two NCS devices.') print('Insert two devices and try again!') return 1 ty_device = mvnc.Device(devices[0]) ty_device.OpenDevice() gn_device = mvnc.Device(devices[1]) gn_device.OpenDevice() #Load tiny yolo graph from disk and allocate graph via API with open(tiny_yolo_graph_file, mode='rb') as ty_file: ty_graph_from_disk = ty_file.read() ty_graph = ty_device.AllocateGraph(ty_graph_from_disk) #Load googlenet graph from disk and allocate graph via API with open(googlenet_graph_file, mode='rb') as gn_file: gn_graph_from_disk = gn_file.read() gn_graph = gn_device.AllocateGraph(gn_graph_from_disk) # GoogLenet initialization EXAMPLES_BASE_DIR = '../../' gn_mean = np.load(EXAMPLES_BASE_DIR + 'data/ilsvrc12/ilsvrc_2012_mean.npy').mean(1).mean(1) # loading the mean file gn_labels_file = EXAMPLES_BASE_DIR + 'data/ilsvrc12/synset_words.txt' gn_labels = np.loadtxt(gn_labels_file, str, delimiter='\t') for label_index in range(0, len(gn_labels)): temp = gn_labels[label_index].split(',')[0].split(' ', 1)[1] gn_labels[label_index] = temp print('Q to quit, or any key to advance to next image') cv2.namedWindow(cv_window_name) for input_image_file in input_image_filename_list : # Read image from file, resize it to network width and height # save a copy in img_cv for display, then convert to float32, normalize (divide by 255), # and finally convert to convert to float16 to pass to LoadTensor as input for an inference input_image = cv2.imread(input_image_file) # resize the image to be a standard width for all images and maintain aspect ratio STANDARD_RESIZE_WIDTH = 800 input_image_width = input_image.shape[1] input_image_height = input_image.shape[0] standard_scale = float(STANDARD_RESIZE_WIDTH) / input_image_width new_width = int(input_image_width * standard_scale) # this should be == STANDARD_RESIZE_WIDTH new_height = int(input_image_height * standard_scale) input_image = cv2.resize(input_image, (new_width, new_height), cv2.INTER_LINEAR) display_image = input_image input_image = cv2.resize(input_image, (TY_NETWORK_IMAGE_WIDTH, TY_NETWORK_IMAGE_HEIGHT), cv2.INTER_LINEAR) input_image = input_image[:, :, ::-1] # convert to RGB input_image = input_image.astype(np.float32) input_image = np.divide(input_image, 255.0) # Load tensor and get result. This executes the inference on the NCS ty_graph.LoadTensor(input_image.astype(np.float16), 'user object') output, userobj = ty_graph.GetResult() # filter out all the objects/boxes that don't meet thresholds filtered_objs = filter_objects(output.astype(np.float32), input_image.shape[1], input_image.shape[0]) # fc27 instead of fc12 for yolo_small get_googlenet_classifications(gn_graph, display_image, filtered_objs) # check if the window has been closed. all properties will return -1.0 # for windows that are closed. If the user has closed the window via the # x on the title bar then we will break out of the loop. we are # getting property aspect ratio but it could probably be any property try: prop_asp = cv2.getWindowProperty(cv_window_name, cv2.WND_PROP_ASPECT_RATIO) except: break; prop_asp = cv2.getWindowProperty(cv_window_name, cv2.WND_PROP_ASPECT_RATIO) if (prop_asp < 0.0): # the property returned was < 0 so assume window was closed by user break; ret_val = display_objects_in_gui(display_image, filtered_objs) if (not ret_val): break # clean up tiny yolo ty_graph.DeallocateGraph() ty_device.CloseDevice() # Clean up googlenet gn_graph.DeallocateGraph() gn_device.CloseDevice() print('Finished')