def outputs(): global numFrames, time stream = io.BytesIO() for i in range(controlFrames): # This returns the stream for the camera to capture to e1 = cv2.getTickCount() yield stream stream.seek(0) data = np.fromstring(stream.getvalue(), dtype=np.uint8) open_cv_image = cv2.imdecode(data, 1) if filtering: filt.image = open_cv_image f, i, c, h = filt.rgbGet(cv2.CHAIN_APPROX_SIMPLE, Constants.VIDEOS_RGB_FILTER_CONSTANTS_1) output = filt.run(filt.image) try: numFrames += 1 # queue.put(open_cv_image) except: print "FULL QUEUE" stream.seek(0) stream.truncate() e2 = cv2.getTickCount() time += (e2-e1)/cv2.getTickFrequency()
def _calculate_metric(self, x, y, transformer, print_row): tickFrequency = cv2.getTickFrequency() var_x = numpy.var(x, axis=0) var_y = numpy.var(y, axis=0) loss_var_x = numpy.mean(var_x) loss_var_y = numpy.mean(var_y) loss = calculate_reconstruction_error(x, y) matches = match_error(x, y) start_tick = cv2.getTickCount() if self.top == 0: correlation = (calculate_mardia(x, y, 0) / x.shape[1]) * 100 else: correlation = (calculate_mardia(x, y, self.top) / self.top) * 100 current_time = cv2.getTickCount() OutputLog().write('calculated correlation, time: {0}'.format(((current_time - start_tick) / tickFrequency)), 'debug') print_row.append(correlation) print_row.append(loss) print_row.append(loss_var_x) print_row.append(loss_var_y) print_row.append(matches) return correlation
def train_data(self, train, target, epochs = 1, log=False): result = numpy.zeros(epochs) len_all_inputs = len(train) for epoch in range(0, epochs): if log: print "Executando Epoca %d..." %(epoch+1) e1 = cv2.getTickCount() for i, inputs in enumerate(train): correct = target[i] output, correct = self.__train(inputs, correct, log) result[epoch] = result[epoch] + numpy.mean((correct - output)**2)/len_all_inputs self.__update() if log: e2 = getTickCount() time = (e2 - e1)/cv2.getTickFrequency() print "Epoca %d Resultado = %f, Tempo de Execucao %f segundos" %(epoch+1,result[epoch], time) return result
def detect_and_draw( img): """ draw a box with opencv on the image around the detected faces and display the output """ from random import randint start_time = cv2.getTickCount() dog_classifier=DogClassifier() print dog_classifier dogs,cats = detectByMuitScaleSlideWindows(img,windowSize=(50,50),classifier=dog_classifier) end_time = cv2.getTickCount() logging.info("time cost:%gms",(end_time-start_time)/cv2.getTickFrequency()*1000.) #dogs = detectObject(img) print "found dogs:",len(dogs) max_rect=(0,0,0,0) for rect in dogs: x,y,w,h=rect if w*h>max_rect[2]*max_rect[3]: max_rect=rect x,y,w,h=max_rect cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,255), 1) print "found cats:",len(cats) max_rect=(0,0,0,0) for rect in cats: x,y,w,h=rect if w*h>max_rect[2]*max_rect[3]: max_rect=rect x,y,w,h=max_rect cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 1) cv2.imshow("result", img) cv2.waitKey(-1)
def bitwise(): img1 = cv2.imread('messi.jpg') img2 = cv2.imread('python.jpg') e1 = cv2.getTickCount() # I want to put logo on top-left corner, So I create a ROI rows,cols,channels = img2.shape roi = img1[0:rows, 0:cols] # Now create a mask of logo and create its inverse mask also img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY) # cv2.threshold(src, thresh, maxval, type[, dst]) → retval, dst ret, mask = cv2.threshold(img2gray, 230, 255, cv2.THRESH_BINARY_INV) #Applies a fixed-level threshold to each array element. mask_inv = cv2.bitwise_not(mask) # Now black-out the area of logo in ROI img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv) # Take only region of logo from logo image. img2_fg = cv2.bitwise_and(img2,img2,mask = mask) # Put logo in ROI and modify the main image dst = cv2.add(img1_bg,img2_fg) img1[0:rows, 0:cols] = dst e2 =cv2.getTickCount() t = (e2- e1)/cv2.getTickFrequency() print t cv2.imshow('res',img1) cv2.waitKey(0) cv2.destroyAllWindows()
def get_state(self, motion_detect): if self.state == 'start_dreaming': self.__dreaming_start() elif self.state == 'fade_dream_to_frame': self.state = 'fading' elif self.state == 'fading': self.__fading() elif motion_detect > self.motion_threshold: self.__on_motion_above_threshold() elif cv2.getTickCount() - self.low_motion_last_time > self.LOW_MOTION_TIMEOUT: self.state = 'waiting' else: if motion_detect > self.LOW_THRESHOLD: self.low_motion_last_time = cv2.getTickCount() if self.motion: self.start_time = cv2.getTickCount() self.motion = False else: if self.state == 'dreaming': how_long = cv2.getTickCount() - self.dream_start if how_long > self.DREAM_OVER: self.state = 'fade_dream_to_frame' elif self.state == 'show_frames': how_long = cv2.getTickCount() - self.start_time if how_long > 7000000000: self.state = 'start_dreaming' return self.state
def run(self): """Run the main loop.""" self._windowManager.createWindow() while self._windowManager.isWindowCreated: self._captureManager.enterFrame() frame = self._captureManager.frame if frame is not None: t = cv2.getTickCount() self._faceTracker.update(frame) faces = self._faceTracker.faces t = cv2.getTickCount() - t print("time taken for detection = %gms" % (t/(cv2.getTickFrequency())*1000.)) # uncomment this line for swapping faces #rects.swapRects(frame, frame, [face.faceRect for face in faces]) #filters.strokeEdges(frame, frame) #self._curveFilter.apply(frame, frame) if self._shouldDrawDebugRects: self._faceTracker.drawDebugRects(frame) self._faceTracker.drawLinesFromCenter(frame) self._captureManager.exitFrame() self._windowManager.processEvents()
def doTask(self, frame): time_now = cv2.getTickCount() time_end = (time_now - self.last_time) / cv2.getTickFrequency() # Record time value in a rolling sum where the oldest falls off # This way we can smooth out the FPS value oldest_val = self.time_records[self.time_rec_idx] if oldest_val > 0: self.time_total -= oldest_val self.time_total += time_end self.time_records[self.time_rec_idx] = time_end self.time_rec_idx = (self.time_rec_idx + 1) % NUM_TIME_RECORDS # Write FPS to frame #cv2.putText(frame, '%2.2f FPS' % (1 / time_end), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255)) cv2.putText(frame, '%2.2f FPS' % (NUM_TIME_RECORDS / self.time_total), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255)) cv2.imshow("Video", frame) self.last_time = cv2.getTickCount() if cv2.waitKey(1) & 0xFF == ord('q'): return False else: return True
def take_snapshot(delay=2): cap = cv2.VideoCapture(0) if not cap.isOpened(): print "Cannot open camera!" return # Set video to 320x240 cap.set(3, 320) cap.set(4, 240) take_picture = False; t0, filenum = 0, 1 while True: val, frame = cap.read() cv2.imshow("video", frame) key = cv2.waitKey(30) if key == ord(' '): t0 = cv2.getTickCount() take_picture = True elif key == ord('q'): break if take_picture and ((cv2.getTickCount()-t0) / cv2.getTickFrequency()) > delay: cv2.imwrite(str(filenum) + ".jpg", frame) filenum += 1 take_picture = False cap.release()
def capture_camera(mirror=True, size=None): """Capture video from camera""" cap = cv2.VideoCapture(0) freq = 1000/cv2.getTickFrequency() start_time = cv2.getTickCount() oldcnt = 0 cnt = 0 while True: now_time = cv2.getTickCount() diff_time = (now_time - start_time)*freq if diff_time > 1000: start_time = now_time fps = cnt - oldcnt oldcnt = cnt print fps ret, frame = cap.read() if mirror is True: frame = frame[:,::-1] if size is not None and len(size) == 2: frame = cv2.resize(frame, size) cv2.imshow('camera capture', frame) k = cv2.waitKey(1) if k == 27: break cnt += 1 cap.release() cv2.destroyAllWindows()
def compute_outputs(self, test_set_x, test_set_y, hyperparameters): hidden_output_model = self._build_hidden_model() recon_model = self._build_reconstruction_model() hidden_values_x = [] hidden_values_y = [] number_of_batches = int(math.ceil(float(test_set_x.shape[0]) / hyperparameters.batch_size)) outputs_hidden = None outputs_recon = None for i in range(number_of_batches): batch_x = test_set_x[ i * hyperparameters.batch_size: min((i + 1) * hyperparameters.batch_size, test_set_x.shape[0]), :] batch_y = test_set_y[ i * hyperparameters.batch_size: min((i + 1) * hyperparameters.batch_size, test_set_y.shape[0]), :] self._correlation_optimizer.var_x.set_value(batch_x) self._correlation_optimizer.var_y.set_value(batch_y) start_tick = cv2.getTickCount() outputs_hidden_batch = hidden_output_model() output_recon_batch = recon_model() tickFrequency = cv2.getTickFrequency() current_time = cv2.getTickCount() OutputLog().write('batch {0}/{1} ended, time: {2}'.format(i, number_of_batches, ((current_time - start_tick) / tickFrequency)), 'debug') if i == 0: outputs_recon = output_recon_batch else: for idx in range(len(outputs_recon)): outputs_recon[idx] = numpy.concatenate((outputs_recon[idx], output_recon_batch[idx]), axis=0) if i == 0: outputs_hidden = outputs_hidden_batch else: for idx in range(len(outputs_hidden)): outputs_hidden[idx] = numpy.concatenate((outputs_hidden[idx], outputs_hidden_batch[idx]), axis=0) for i in xrange(len(outputs_hidden) / 2): hidden_values_x.append(outputs_hidden[2 * i]) hidden_values_y.append(outputs_hidden[2 * i + 1]) # for i in xrange(len(outputs_recon) / 2): # hidden_values_x.append(outputs_recon[2 * i]) # hidden_values_y.append(outputs_recon[2 * i + 1]) # if hidden_values_x[0].shape[1] == hidden_values_y[-1].shape[1] and len(hidden_values_x) > 1: # hidden_values_x.append(hidden_values_x[-1]) # hidden_values_y.append(hidden_values_y[0]) return [hidden_values_x, hidden_values_y]
def serial_continuous(self, verbose=False): # assuming only one processor doing work # so kill others if they exist (they shouldn't though) if self.rank != 0: return self.log("Serial execution, continuous, action={0}".format(self.action)) start_time = getTickCount() clip_num = 0 while True: if verbose: print "Clip: {0}".format(clip_num) clip_num += 1 in_frames = self.vid.read_next_clip() self.vid.write_frame_block(self.do_action(in_frames)) # end on last frame block if len(in_frames) < self.vid.clip_size: break self.vid.destroy() end_time = getTickCount() comp_time = (end_time - start_time) / getTickFrequency() self.log("Computation time: {0} s".format(comp_time))
def WeinerFilter(): e1 = cv2.getTickCount() astro = color.rgb2gray(data.imread(imagePath)) psf = np.ones((5, 5)) / 25 astro = conv2(astro, psf, 'same') astro += 0.1 * astro.std() * np.random.standard_normal(astro.shape) deconvolved, _ = restoration.unsupervised_wiener(astro, psf) fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(8, 5), sharex=True, sharey=True, subplot_kw={'adjustable':'box-forced'}) plt.gray() ax[0].imshow(astro, vmin=deconvolved.min(), vmax=deconvolved.max()) ax[0].axis('off') ax[0].set_title('Original Picture') ax[1].imshow(deconvolved) ax[1].axis('off') ax[1].set_title('Self tuned restoration') fig.subplots_adjust(wspace=0.02, hspace=0.2, top=0.9, bottom=0.05, left=0, right=1) e2 = cv2.getTickCount() time = (e2 - e1)/ cv2.getTickFrequency() + (numberThreads) + numberThreadsPerCore + numberCores msgbox(msg= str(time) +" seconds", title="Execution Time", ok_button="OK") plt.show()
def detect_by_slide_windows( img): """ draw a box with opencv on the image around the detected faces and display the output """ from random import randint start_time = cv2.getTickCount() dogcat_classifier=MultiClassifier() print dogcat_classifier img=cv2.resize(img,(100,100)) dogs,cats = check_multiLocation_multiScale_windows(img,windowSize=(50,50),classifier=dogcat_classifier) end_time = cv2.getTickCount() logging.info("time cost:%gms",(end_time-start_time)/cv2.getTickFrequency()*1000.) print "found dogs:",len(dogs) max_rect=(0,0,0,0) sum_rect=numpy.asarray((0,0,0,0),dtype=numpy.float) for rect in dogs: x,y,w,h=rect sum_rect +=rect if w*h>max_rect[2]*max_rect[3]: max_rect=rect #cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,255), 1) #x,y,w,h=max_rect x,y,w,h=numpy.asarray(sum_rect/len(dogs),dtype=numpy.uint) cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,255), 1) print "found cats:",len(cats) max_rect=(0,0,0,0) for rect in cats: x,y,w,h=rect if w*h>max_rect[2]*max_rect[3]: max_rect=rect x,y,w,h=max_rect cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 1) return img
def Producer(): global timeCapture, numFrames, time try: print "STARTED TO PRODUCE" tot1 = cv2.getTickCount() for frame in camera.capture_continuous(capture, format='bgr', use_video_port=True): # e1 = cv2.getTickCount() # QUEUE FULL EXCEPTION try: queue.put(frame.array) # print "PUT AN IMAGE" except: print "TOO BIG!" # e2 = cv2.getTickCount() # timeCapture += (e2-e1) / cv2.getTickFrequency() capture.truncate(0) if controlFrames != 0 and numFrames >= controlFrames: print "STOPPED PRODUCING" break else: # print numFrames numFrames += 1 except KeyboardInterrupt: # IT HAS ENDED pass tot2 = cv2.getTickCount() total = (tot2 - tot1) / cv2.getTickFrequency() cv2.destroyAllWindows() camera.close() time = 1.0 # CAUSE TIME IS BROKEN RIGHT NOW timeCapture = 1.0 # CAUSE TIME IS COMMENTED OUT ABOVE
def action(self): ''' invoking entry function ''' cap = cv2.VideoCapture(self.video_index) if not cap.isOpened(): print('ERROR: cannot open video capture device...') cap.release() return self.init_window() cap.set(cv2.CAP_PROP_FRAME_WIDTH, self.width) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, self.height) print("press 'q' to quit") while True: # Capture frame-by-frame ret, frame = cap.read() if not ret: break if self.has_skin: self.skin_mask(frame) if self.win_count == 0: #rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) cv2.imshow('frame', frame) else: # Our operations on the frame come here if self.has_gray: gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.imshow('gray', gray) if self.has_rgb: #rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) rgb = frame cv2.imshow('rgb', rgb) if self.has_test: ifrm = frame.copy() e0 = cv2.getTickCount() cvimg = hough_lines(ifrm) e1 = cv2.getTickCount() elapsed = (e1 - e0) / cv2.getTickFrequency() show_fps(cvimg, elapsed) #blue = self.split_blue(ifrm) cv2.imshow('test', cvimg) if self.has_hsv: hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) cv2.imshow('hsv', hsv) key = cv2.waitKey(1) if key & 0xFF == ord('q') or key == 27: break # When everything done, release the capture cap.release() cv2.destroyAllWindows()
def main(): """ Program entry point. Handles high-level interfacing of video and scene detection / output. """ # Get command line arguments directly from the CLI parser defined above. args = get_cli_parser().parse_args() # Attempt to open the passed video file as an OpenCV VideoCapture object. cap = cv2.VideoCapture() cap.open(args.input.name) if not cap.isOpened(): print 'FATAL ERROR - could not open video %s.' % args.input.name print 'cap.isOpened() is not True after calling cap.open(..)' return else: print 'Parsing video %s...' % args.input.name # Print video parameters (resolution, FPS, etc...) video_width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) video_height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) video_fps = cap.get(cv2.CAP_PROP_FPS) print 'Video Resolution / Framerate: %d x %d / %2.3f FPS' % ( video_width, video_height, video_fps ) start_time = cv2.getTickCount() # Record the time we started processing. if (args.statsfile): # Only generate statistics, to help setting further parameters. generate_video_stats(cap, args.statsfile) else: # Perform threshold analysis on video, get list of fades in/out. fade_list = analyze_video_threshold( cap, args.threshold, args.minpercent, args.blocksize ) # Get # of frames based on position of last frame we read. frame_count = cap.get(cv2.CAP_PROP_POS_FRAMES) # Compute & display number of frames, runtime, and average framerate. total_runtime = float(cv2.getTickCount() - start_time) / cv2.getTickFrequency() avg_framerate = float(frame_count) / total_runtime print 'Read %d frames in %4.2f seconds (avg. %4.1f FPS).' % ( frame_count, total_runtime, avg_framerate ) # Ensure we actually detected anything from the video file. if not len(fade_list) > 0: print 'Error - no fades detected in video!' else: # Generate list of scenes from fades, writing to CSV output if specified. scene_list = generate_scene_list(cap, fade_list, args.output) print 'Detected %d scenes in video.' % len(scene_list) # Cleanup (release all memory and close file handles). cap.release() if (args.output): args.output.close() if (args.statsfile): args.statsfile.close() print ''
def test_medianBlur(img): """docstring for test_medianBlur""" e1 = cv2.getTickCount() for i in xrange(5, 49, 2): img = cv2.medianBlur(img, i) e2 = cv2.getTickCount() t = (e2 - e1) / cv2.getTickFrequency() print t
def send(self, obj, dest, tag): start = getTickCount() # MPI Send self.comm.send(obj, dest=dest, tag=tag) end = getTickCount() self.comm_times += (end - start) / getTickFrequency()
def __get_next_frame(self): delta_t = 0.0 while delta_t < 30000000.0: t = cv2.getTickCount() self.video_capture.grab() delta_t = cv2.getTickCount() - t rt, frm = self.video_capture.retrieve() self.last_frame = frm return frm
def mclass(cls1,cls2,cls3): imax,jmax=cls1.shape cls=np.zeros(imax*jmax).reshape(imax,jmax) t=cv2.getTickCount() for i in range(imax): for j in range(jmax): cls[i,j]=cls1[i,j]+cls2[i,j]*30+cls3[i,j]*900 print (cv2.getTickCount()-t)/cv2.getTickFrequency() return np.uint16(cls)
def TemporalAlignment(captureQ, captureR): global harlocsQ, harlocsR; # if compute_harris == 1:... totalT1 = float(cv2.getTickCount()); #if config.PREPROCESS_REFERENCE_VIDEO_ONLY == True: # This will give error in multiscale_quad_retrieval since we need to load the saved Harris features anyhow if True: # We compute and Store in files the multi-scale Harris features of the reference video """ harlocsR = ComputeHarlocs(captureR, config.counterRStep, \ folderName="/harlocs_ref", fileNamePrefix="harloc"); """ harlocsR = ComputeHarlocs(captureR, config.counterRStep, \ folderName=config.HARRIS_REFERENCE_FOLDER_NAME, \ fileNamePrefix=config.HARRIS_FILENAME_PREFIX, \ fileNameExtension=config.HARRIS_FILENAME_EXTENSION, indexVideo=1); if common.MY_DEBUG_STDOUT: common.DebugPrint("TemporalAlignment(): len(harlocsR) = %s" % str(len(harlocsR))); sumNbytes = 0; for hr in harlocsR: sumNbytes += hr.nbytes; common.DebugPrint("TemporalAlignment(): harlocsR.nbytes = %s" % str(sumNbytes)); #common.DebugPrint("TemporalAlignment(): harlocsR.nbytes = %s" % str(harlocsR.nbytes)); if config.PREPROCESS_REFERENCE_VIDEO_ONLY == False: # We compute and Store in files the multi-scale Harris features of the query video """ Note: if the "harloc" files exist, load directly the features from the files without computing them again. """ harlocsQ = ComputeHarlocs(captureQ, config.counterQStep, \ folderName=config.HARRIS_QUERY_FOLDER_NAME, \ fileNamePrefix=config.HARRIS_FILENAME_PREFIX, \ fileNameExtension=config.HARRIS_FILENAME_EXTENSION, indexVideo=0); if common.MY_DEBUG_STDOUT: common.DebugPrint("TemporalAlignment(): len(harlocsQ) = %s" % \ str(len(harlocsQ))); sumNbytes = 0; for hq in harlocsQ: sumNbytes += hq.nbytes; common.DebugPrint("TemporalAlignment(): harlocsQ.nbytes = %s" % str(sumNbytes)); #res = QuadTreeDecision(captureQ, captureR); res = QuadTreeDecision(); else: res = None totalT2 = float(cv2.getTickCount()); myTime = (totalT2 - totalT1) / cv2.getTickFrequency(); print("TemporalAlignment() took %.6f [sec]" % (myTime)); return res;
def __on_faces_present(self): if not self.faces: if self.state == 'waiting': self.start_time = cv2.getTickCount() elif cv2.getTickCount() - self.start_time > self.FACES_DELAY_TIMER: if self.state != 'dreaming': self.state = 'start_dreaming' self.faces = True self.faces_latest_time = cv2.getTickCount()
def testPicture(background, inputPic, outputFileName = 'testPicture_out.png'): t1 = cv2.getTickCount() output = magic(inputPic,background)#, tolerance=[10,20]) t2 = cv2.getTickCount() print 'testPicture processed in {0} s'.format((t2 - t1)/cv2.getTickFrequency()) cv2.imwrite(outputFileName, output)
def match(self, grey1, grey2): word = None #start tracking time for detection e1 = cv2.getTickCount() kpA, descA = self.detector.detectAndCompute(grey1, word) kpB, descB = self.detector.detectAndCompute(grey2, word) e2 = cv2.getTickCount() time = (e2 - e1)/ cv2.getTickFrequency() return self.bf_matching(kpA, descA, kpB, descB, time)
def medianFilter(): e1 = cv2.getTickCount() source = cv2.imread(imagePath) final = cv2.medianBlur(source, 9) e2 = cv2.getTickCount() time = (e2 - e1)/ cv2.getTickFrequency() + (numberThreads) + numberThreadsPerCore + numberCores msgbox(msg= str(time) +" seconds", title="Execution Time", ok_button="OK") cv2.imshow('Original Picture', source) #Show the image cv2.imshow('Median Filtered Image', final) #Show the image cv2.waitKey() cv2.destroyAllWindows()
def update(self, DEBUG=False, ShowPerformance=False): def distance(pixel): return 84134.5 - 0.0390032 * pixel - 53553.8 * np.arctan(pixel ** 2) def drawReferenceLines(): # 90cm cv2.line(outputImg, (0, 26), (w, 26), (255)) # 60cm cv2.line(outputImg, (0, 33), (w, 33), (180)) # 30cm cv2.line(outputImg, (0, 55), (w, 55), (128)) def drawLine(row, column): cv2.line(outputImg, (column, row), (column, h), (255)) last = cv2.getTickCount() while True: image = q.get() if image is None: time.sleep(0.1) continue grayImg = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) outputImg = cv2.threshold(grayImg, 160, 255, cv2.THRESH_BINARY)[1] values = [] for columnIndex in range(0, w, 2): column = outputImg[:, columnIndex] row = np.nonzero(column)[0] if not row.size == 0: values.append((columnIndex, distance(row[0]))) if DEBUG: drawLine(row[0], columnIndex) else: values.append((columnIndex, 300)) self.values = values if DEBUG: drawReferenceLines() cv2.imshow("image", grayImg) cv2.imshow('outputImg', outputImg) cv2.waitKey(1) if ShowPerformance: timeSpend = cv2.getTickCount() - last last = cv2.getTickCount() timeSec = timeSpend / cv2.getTickFrequency() print(timeSec, 1 / timeSec)
def main(): img = open_image('../text.bmp') e1 = cv2.getTickCount() img = perform_changes_task2(img) e2 = cv2.getTickCount() time = (e2 - e1) / cv2.getTickFrequency() show_image(img) print "time elapsed: ", time wait_exit()
def main(): img = open_image('../text.bmp') e1 = cv2.getTickCount() img, orig = filter_words(img) e2 = cv2.getTickCount() time = (e2 - e1) / cv2.getTickFrequency() show_image(img, orig) print "time elapsed: ", time wait_exit()
def main(): img1 = cv2.imread('1.jpg') e1 = cv2.getTickCount() t1 = time.time() for i in range(5, 49, 2): img1 = cv2.medianBlur(img1, i) # your code execution e2 = cv2.getTickCount() t2 = time.time() diff_e = (e2 - e1) / cv2.getTickFrequency() print(diff_e) print(t2 - t1)
# if video file successfully open then read frame from video if (cap.isOpened): ret, frame = cap.read() # rescale if specified if (args.rescale != 1.0): frame = cv2.resize(frame, (0, 0), fx=args.rescale, fy=args.rescale) # start a timer (to see how long processing and display takes) start_t = cv2.getTickCount() # get parameters from track bars s_lower = cv2.getTrackbarPos("s lower", windowName2) s_upper = cv2.getTrackbarPos("s upper", windowName2) v_lower = cv2.getTrackbarPos("v lower", windowName2) v_upper = cv2.getTrackbarPos("v upper", windowName2) # select region using the mouse and display it if (len(boxes) > 1) and (boxes[0][1] < boxes[1][1]) and (boxes[0][0] < boxes[1][0]): crop = frame[boxes[0][1]:boxes[1][1], boxes[0][0]:boxes[1][0]].copy()
def detect_objects(image_np, sess, detection_graph, image, depth, point_cloud, category_index): # Expand dimensions since the model expects images to have shape: [1, None, None, 3] image_np_expanded = np.expand_dims(image_np, axis=0) image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') # Each box represents a part of the image where a particular object was detected. boxes = detection_graph.get_tensor_by_name('detection_boxes:0') # Each score represent how level of confidence for each of the objects. # Score is shown on the result image, together with the class label. scores = detection_graph.get_tensor_by_name('detection_scores:0') classes = detection_graph.get_tensor_by_name('detection_classes:0') num_detections = detection_graph.get_tensor_by_name('num_detections:0') t1 = cv2.getTickCount() # Actual detection. out = sess.run([boxes, scores, classes, num_detections], feed_dict={image_tensor: image_np_expanded}) (boxes, scores, classes, num_detections) = out centerx = 0 centery = 0 key = '' rows = image_np.shape[0] cols = image_np.shape[1] num_detections = int(num_detections) for i in range(num_detections): classId = (out[2][0][i]) score = float(out[1][0][i]) bbox = [float(v) for v in out[0][0][i]] if score > 0.3: x = bbox[1] * cols y = bbox[0] * rows right = bbox[3] * cols bottom = bbox[2] * rows #cv2.rectangle(img, (int(x), int(y)), (int(right), int(bottom)), (125, 255, 51), thickness=1) cv2.circle(image_np, (int((x + right) / 2), int((y + bottom) / 2)), 8, (125, 155, 21), thickness=1) centerx = int((x + right) / 2) centery = int((y + bottom) / 2) # Get and print distance value in mm at the center of the object # We measure the distance camera - object using Euclidean distance err, point_cloud_value = point_cloud.get_value(centerx, centery) distance = math.sqrt(point_cloud_value[0] * point_cloud_value[0] + point_cloud_value[1] * point_cloud_value[1] + point_cloud_value[2] * point_cloud_value[2]) if not np.isnan(distance) and not np.isinf(distance): distance = round(distance) #print("object {0} Distance to Camera at ({1}, {2}): {3} mm\n".format(i,x, y, distance)) classes = np.squeeze(classes).astype(np.int32) if classes[i] in category_index.keys(): class_name = category_index[classes[i]]['name'] else: class_name = 'N/A' display_str = str(class_name) cv2.putText(image_np, "{0}:{1} mm".format(display_str, distance), (int(centerx), int(centery)), cv2.FONT_HERSHEY_SIMPLEX, 1, (125, 155, 21), thickness=2, lineType=2) # Increment the loop else: print( "Can't estimate distance at this position, move the camera\n" ) sys.stdout.flush() t2 = cv2.getTickCount() print((t2 - t1) / cv2.getTickFrequency()) return image_np
def main(): model_name = 'siamRPN' snapshot_path = os.path.join(project_path_, 'experiments/%s/model.pth' % args.tracker_name) config_path = os.path.join(project_path_, 'experiments/%s/config.yaml' % args.tracker_name) cfg.merge_from_file(config_path) # create model model = ModelBuilder() # a model is a Neural Network.(a torch.nn.Module) # load model model = load_pretrain(model, snapshot_path).cuda().eval() # build tracker tracker = build_tracker(model) # a tracker is a object consisting of not only a NN and some post-processing # create dataset dataset = DatasetFactory.create_dataset(name=args.dataset, dataset_root=dataset_root_, load_img=False) total_lost = 0 if args.dataset in ['VOT2016', 'VOT2018', 'VOT2019']: # restart tracking for v_idx, video in enumerate(dataset): if args.video != '': # test one special video if video.name != args.video: continue frame_counter = 0 lost_number = 0 toc = 0 pred_bboxes = [] for idx, (img, gt_bbox) in enumerate(video): if len(gt_bbox) == 4: gt_bbox = [gt_bbox[0], gt_bbox[1], gt_bbox[0], gt_bbox[1]+gt_bbox[3]-1, gt_bbox[0]+gt_bbox[2]-1, gt_bbox[1]+gt_bbox[3]-1, gt_bbox[0]+gt_bbox[2]-1, gt_bbox[1]] tic = cv2.getTickCount() if idx == frame_counter: H,W,_ = img.shape cx, cy, w, h = get_axis_aligned_bbox(np.array(gt_bbox)) gt_bbox_ = [cx-(w-1)/2, cy-(h-1)/2, w, h] tracker.init(img, gt_bbox_) pred_bbox = gt_bbox_ pred_bboxes.append(1) elif idx > frame_counter: outputs = tracker.track(img) pred_bbox = outputs['bbox'] overlap = vot_overlap(pred_bbox, gt_bbox, (img.shape[1], img.shape[0])) if overlap > 0: # not lost pred_bboxes.append(pred_bbox) else: # lost object pred_bboxes.append(2) frame_counter = idx + 5 # skip 5 frames lost_number += 1 else: pred_bboxes.append(0) toc += cv2.getTickCount() - tic if idx == 0: cv2.destroyAllWindows() if args.vis and idx > frame_counter: cv2.polylines(img, [np.array(gt_bbox, np.int).reshape((-1, 1, 2))], True, (0, 255, 0), 3) bbox = list(map(int, pred_bbox)) cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[0]+bbox[2], bbox[1]+bbox[3]), (0, 255, 255), 3) cv2.putText(img, str(idx), (40, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2) cv2.putText(img, str(lost_number), (40, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) cv2.imshow(video.name, img) cv2.waitKey(1) toc /= cv2.getTickFrequency() # save results video_path = os.path.join(save_dir, args.dataset, model_name, 'baseline', video.name) if not os.path.isdir(video_path): os.makedirs(video_path) result_path = os.path.join(video_path, '{}_001.txt'.format(video.name)) with open(result_path, 'w') as f: for x in pred_bboxes: if isinstance(x, int): f.write("{:d}\n".format(x)) else: f.write(','.join([vot_float2str("%.4f", i) for i in x])+'\n') print('({:3d}) Video: {:12s} Time: {:4.1f}s Speed: {:3.1f}fps Lost: {:d}'.format( v_idx+1, video.name, toc, idx / toc, lost_number))
def track(tracker, net, video, args): start_frame, lost_times, toc = 0, 0, 0 # save result to evaluate if args.epoch_test: suffix = args.resume.split('/')[-1] suffix = suffix.split('.')[0] tracker_path = os.path.join('result', args.dataset, args.arch + suffix) else: tracker_path = os.path.join('result', args.dataset, args.arch) if not os.path.exists(tracker_path): os.makedirs(tracker_path) if 'VOT' in args.dataset: baseline_path = join(tracker_path, 'baseline') video_path = join(baseline_path, video['name']) if not os.path.exists(video_path): os.makedirs(video_path) result_path = join(video_path, video['name'] + '_001.txt') else: result_path = join(tracker_path, '{:s}.txt'.format(video['name'])) if os.path.exists(result_path): return 0 # for mult-gputesting regions = [] # result and states[1 init / 2 lost / 0 skip] image_files, gt = video['image_files'], video['gt'] for f, image_file in enumerate(image_files): im = cv2.imread(image_file) if len(im.shape) == 2: im = cv2.cvtColor(im, cv2.COLOR_GRAY2BGR) tic = cv2.getTickCount() if f == start_frame: # init cx, cy, w, h = get_axis_aligned_bbox(gt[f]) target_pos = np.array([cx, cy]) target_sz = np.array([w, h]) state = tracker.init(im, target_pos, target_sz, net) # init tracker regions.append(1 if 'VOT' in args.dataset else gt[f]) elif f > start_frame: # tracking state = tracker.track(state, im) # track location = cxy_wh_2_rect(state['target_pos'], state['target_sz']) b_overlap = poly_iou(gt[f], location) if 'VOT' in args.dataset else 1 if b_overlap > 0: regions.append(location) else: regions.append(2) lost_times += 1 start_frame = f + 5 # skip 5 frames else: # skip regions.append(0) toc += cv2.getTickCount() - tic toc /= cv2.getTickFrequency() with open(result_path, "w") as fin: if 'VOT' in args.dataset: for x in regions: if isinstance(x, int): fin.write("{:d}\n".format(x)) else: p_bbox = x.copy() fin.write(','.join([str(i) for i in p_bbox]) + '\n') else: for x in regions: p_bbox = x.copy() fin.write(','.join([ str(i + 1) if idx == 0 or idx == 1 else str(i) for idx, i in enumerate(p_bbox) ]) + '\n') print('Video: {:12s} Time: {:2.1f}s Speed: {:3.1f}fps Lost: {:d}'.format( video['name'], toc, f / toc, lost_times)) return lost_times
cap = cv2.VideoCapture(1) # TRACKER INITIALIZATION success, frame = cap.read() bbox = cv2.selectROI("Tracking",frame, False) tracker.init(frame, bbox) def drawBox(img,bbox): x, y, w, h = int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3]) cv2.rectangle(img, (x, y), ((x + w), (y + h)), (255, 0, 255), 3, 3 ) cv2.putText(img, "Tracking", (100, 75), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) while True: timer = cv2.getTickCount() success, img = cap.read() success, bbox = tracker.update(img) if success: drawBox(img,bbox) else: cv2.putText(img, "Lost", (100, 75), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) cv2.rectangle(img,(15,15),(200,90),(255,0,255),2) cv2.putText(img, "Fps:", (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,0,255), 2); cv2.putText(img, "Status:", (20, 75), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 255), 2); fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer); if fps>60: myColor = (20,230,20)
def count(self, frame): _timer = cv2.getTickCount( ) # set timer to calculate processing frame rate self.frame = frame for _id, blob in list(self.blobs.items()): # update trackers success, box = blob.tracker.update(self.frame) if success: blob.num_consecutive_tracking_failures = 0 blob.update(box) logger.debug('Vehicle tracker updated.', extra={ 'meta': { 'cat': 'TRACKER_UPDATE', 'vehicle_id': _id, 'bounding_box': blob.bounding_box, 'centroid': blob.centroid, }, }) else: blob.num_consecutive_tracking_failures += 1 # count vehicles that have left the frame if no counting line exists # or those that have passed the counting line if one exists if (self.counting_line == None and \ (blob.num_consecutive_tracking_failures == self.mctf or blob.num_consecutive_detection_failures == self.mcdf) and \ not blob.counted) \ or \ (self.counting_line != None and \ # don't count a blob if it was first detected at a position past the counting line # this enforces counting in only one direction not is_passed_counting_line(blob.position_first_detected, self.counting_line, self.cl_position) and \ is_passed_counting_line(blob.centroid, self.counting_line, self.cl_position) and \ not blob.counted): blob.counted = True self.vehicle_count += 1 # count by vehicle type if blob.type != None: if blob.type in self.types_counts: self.types_counts[blob.type] += 1 else: self.types_counts[blob.type] = 1 logger.info('Vehicle counted.', extra={ 'meta': { 'cat': 'VEHICLE_COUNT', 'id': _id, 'type': blob.type, 'count': self.vehicle_count, 'position_first_detected': blob.position_first_detected, 'position_counted': blob.centroid, 'counted_at': time.time(), }, }) if blob.num_consecutive_tracking_failures >= self.mctf: # delete untracked blobs del self.blobs[_id] if self.frame_count >= self.di: # rerun detection droi_frame = get_roi_frame(self.frame, self.droi) _bounding_boxes, _classes, _confidences = get_bounding_boxes( droi_frame, self.detector) self.blobs = add_new_blobs(_bounding_boxes, _classes, _confidences, self.blobs, self.frame, self.tracker, self.counting_line, self.cl_position, self.mcdf) self.blobs = remove_duplicates(self.blobs) self.frame_count = 0 self.frame_count += 1 self.processing_frame_rate = round( cv2.getTickFrequency() / (cv2.getTickCount() - _timer), 2) logger.debug('Processing frame rate updated.', extra={ 'meta': { 'cat': 'PROCESSING_SPEED', 'frame_rate': self.processing_frame_rate }, })
def run_tracker(tracker, img, gt, video_name, restart=True): frame_counter = 0 lost_number = 0 toc = 0 pred_bboxes = [] if restart: # VOT2016 and VOT 2018 for idx, (img, gt_bbox) in enumerate(video): if len(gt_bbox) == 4: gt_bbox = [ gt_bbox[0], gt_bbox[1], gt_bbox[0], gt_bbox[1] + gt_bbox[3] - 1, gt_bbox[0] + gt_bbox[2] - 1, gt_bbox[1] + gt_bbox[3] - 1, gt_bbox[0] + gt_bbox[2] - 1, gt_bbox[1] ] tic = cv2.getTickCount() if idx == frame_counter: cx, cy, w, h = get_axis_aligned_bbox(np.array(gt_bbox)) gt_bbox_ = [cx - (w - 1) / 2, cy - (h - 1) / 2, w, h] tracker.init(img, gt_bbox_) pred_bbox = gt_bbox_ pred_bboxes.append(1) elif idx > frame_counter: outputs = tracker.track(img) pred_bbox = outputs['bbox'] overlap = vot_overlap(pred_bbox, gt_bbox, (img.shape[1], img.shape[0])) if overlap > 0: # not lost pred_bboxes.append(pred_bbox) else: # lost object pred_bboxes.append(2) frame_counter = idx + 5 # skip 5 frames lost_number += 1 else: pred_bboxes.append(0) toc += cv2.getTickCount() - tic toc /= cv2.getTickFrequency() print( 'Video: {:12s} Time: {:4.1f}s Speed: {:3.1f}fps Lost: {:d}'.format( video_name, toc, idx / toc, lost_number)) return pred_bboxes else: toc = 0 pred_bboxes = [] scores = [] track_times = [] for idx, (img, gt_bbox) in enumerate(video): tic = cv2.getTickCount() if idx == 0: cx, cy, w, h = get_axis_aligned_bbox(np.array(gt_bbox)) gt_bbox_ = [cx - (w - 1) / 2, cy - (h - 1) / 2, w, h] tracker.init(img, gt_bbox_) pred_bbox = gt_bbox_ scores.append(None) pred_bboxes.append(pred_bbox) else: outputs = tracker.track(img) pred_bbox = outputs['bbox'] pred_bboxes.append(pred_bbox) scores.append(outputs['best_score']) toc += cv2.getTickCount() - tic track_times.append( (cv2.getTickCount() - tic) / cv2.getTickFrequency()) toc /= cv2.getTickFrequency() print('Video: {:12s} Time: {:5.1f}s Speed: {:3.1f}fps'.format( video_name, toc, idx / toc)) return pred_bboxes, scores, track_times
import numpy as np import cv2 from matplotlib import pyplot as plt #Lecture image en niveau de gris et conversion en float64 img = np.float64(cv2.imread('../Image_Pairs/FlowerGarden2.png', 0)) (h, w) = img.shape print("Dimension de l'image :", h, "lignes x", w, "colonnes") #Méthode directe t1 = cv2.getTickCount() img2 = cv2.copyMakeBorder(img, 0, 0, 0, 0, cv2.BORDER_REPLICATE) for y in range(1, h - 1): for x in range(1, w - 1): val = 5 * img[y, x] - img[y - 1, x] - img[y, x - 1] - img[y + 1, x] - img[y, x + 1] img2[y, x] = min(max(val, 0), 255) t2 = cv2.getTickCount() time = (t2 - t1) / cv2.getTickFrequency() print("Méthode directe :", time, "s") plt.subplot(121) plt.imshow(img2, cmap='gray') plt.title('Convolution - Méthode Directe') #Méthode filter2D t1 = cv2.getTickCount() kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) img3 = cv2.filter2D(img, -1, kernel)
def main(): # Create the in-memory stream stream = io.BytesIO() # Create a window cv2.namedWindow(WINDOW_DISPLAY_IMAGE) # position the window cv2.moveWindow(WINDOW_DISPLAY_IMAGE, 0, 35) # Add some controls to the window cv2.createTrackbar(CONTROL_SCAN_RADIUS, WINDOW_DISPLAY_IMAGE, 5, 50, onScanRadiusChange) cv2.setTrackbarPos(CONTROL_SCAN_RADIUS, WINDOW_DISPLAY_IMAGE, SCAN_RADIUS_REG) cv2.createTrackbar(CONTROL_NUMBER_OF_CIRCLES, WINDOW_DISPLAY_IMAGE, 0, 7, onCircleScanChange) cv2.setTrackbarPos(CONTROL_NUMBER_OF_CIRCLES, WINDOW_DISPLAY_IMAGE, NUMBER_OF_CIRCLES) cv2.createTrackbar(CONTROL_LINE_WIDTH, WINDOW_DISPLAY_IMAGE, 0, RESOLUTION_X, onLineWidthChange) cv2.setTrackbarPos(CONTROL_LINE_WIDTH, WINDOW_DISPLAY_IMAGE, SCAN_RADIUS * 2) returnString = """ Press Esc to end the program Using camera resolution: {}x{} Centre point: {}:{} Scan radius: {} Number of search itterations: {} Baseline Width: {} """.format(RESOLUTION_X, RESOLUTION_Y, SCAN_POS_X, SCAN_HEIGHT, SCAN_RADIUS_REG, NUMBER_OF_CIRCLES, SCAN_RADIUS * 2) print(returnString) # Open connection to camera with picamera.PiCamera() as camera: # Set camera resolution camera.resolution = (RESOLUTION_X, RESOLUTION_Y) # Start loop while True: # Get the tick count so we can keep track of performance e1 = cv2.getTickCount() # Capture image from camera camera.capture(stream, format='jpeg', use_video_port=True) # Convert image from camera to a numpy array data = np.fromstring(stream.getvalue(), dtype=np.uint8) # Decode the numpy array image image = cv2.imdecode(data, cv2.CV_LOAD_IMAGE_COLOR) # Empty and return the in-memory stream to beginning stream.seek(0) stream.truncate(0) # Create other images grey_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) display_image = cv2.copyMakeBorder(image, 0, 0, 0, 0, cv2.BORDER_REPLICATE) center_point = (SCAN_POS_X, SCAN_HEIGHT) # San a horizontal line based on the centre point # We could just use this data to work out how far off centre we are and steer accordingly. # Get a data array of all the falues along that line # scan_data is an array containing: # - pixel value scan_data = scanLine(grey_image, display_image, center_point, SCAN_RADIUS) # The center point we believe the line we are following intersects with our scan line. point_on_line = findLine(display_image, scan_data, SCAN_POS_X, SCAN_HEIGHT, SCAN_RADIUS) # Start scanning the arcs # This allows us to look ahead further ahead at the line and work out an angle to steer # From the intersection point of the line, scan in an arc to find the line # The scan data contains an array # - pixel value # - x co-ordinate # - y co-ordinate returnVal, scan_data = scanCircle(grey_image, display_image, point_on_line, SCAN_RADIUS_REG, -90) previous_point = point_on_line # in the same way ads the findLine, go through the data, find the mid point and return the co-ordinates. last_point = findInCircle(display_image, scan_data) cv2.line(display_image, (previous_point[0], previous_point[1]), (last_point[0], last_point[1]), (255, 255, 255), 1) actual_number_of_circles = 0 for scan_count in range(0, NUMBER_OF_CIRCLES): returnVal, scan_data = scanCircle( grey_image, display_image, last_point, SCAN_RADIUS_REG, lineAngle(previous_point, last_point)) # Only work out the next itteration if our point is within the bounds of the image if returnVal == True: actual_number_of_circles += 1 previous_point = last_point last_point = findInCircle(display_image, scan_data) cv2.line(display_image, (previous_point[0], previous_point[1]), (last_point[0], last_point[1]), (255, 255, 255), 1) else: break # Draw a line from the centre point to the end point where we last found the line we are following cv2.line(display_image, (center_point[0], center_point[1]), (last_point[0], last_point[1]), (0, 0, 255), 1) # Display the image cv2.imshow(WINDOW_DISPLAY_IMAGE, display_image) # This is the maximum distance the end point of our search for a line can be from the centre point. line_scan_length = SCAN_RADIUS_REG * (actual_number_of_circles + 1) # This is the measured line length from the centre point line_length_from_center = lineLength(center_point, last_point) center_y_distance = center_point[1] - last_point[1] center_x_distance = center_point[0] - last_point[0] # Stop counting all work is done at this point and calculate how we are doing. e2 = cv2.getTickCount() returnString = "fps {} - bearing {} - x:{} y:{} look distance:{} distance from origin:{}".format( 1000 / ((e2 - e1) / cv2.getTickFrequency() * 1000), lineAngle(center_point, last_point) * -1 - 90, center_x_distance, center_y_distance, line_scan_length, line_length_from_center) print(returnString) # Wait for ESC to end program c = cv2.waitKey(7) % 0x100 if c == 27: break print "Closing program" cv2.destroyAllWindows() print "All windows should be closed" return
def clock(): return cv2.getTickCount() / cv2.getTickFrequency()
def _infer(self, prep_img): t0 = cv2.getTickCount() output = self._exec_model.infer(inputs={self._input_layer_name: prep_img}) self.infer_time = (cv2.getTickCount() - t0) / cv2.getTickFrequency() return output
def calibrate_stereo_cameras(res_x=img_width, res_y=img_height): # We need a lot of variables to calibrate the stereo camera """ Based on code from: https://gist.github.com/aarmea/629e59ac7b640a60340145809b1c9013 """ processing_time01 = cv2.getTickCount() objectPoints = None rightImagePoints = None rightCameraMatrix = None rightDistortionCoefficients = None leftImagePoints = None leftCameraMatrix = None leftDistortionCoefficients = None rotationMatrix = None translationVector = None imageSize = (res_x, res_y) TERMINATION_CRITERIA = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.01) OPTIMIZE_ALPHA = 0.25 try: npz_file = np.load( './calibration_data/{}p/stereo_camera_calibration.npz'.format( res_y)) except: pass for cam_num in [0, 1]: right_or_left = ["_right" if cam_num == 1 else "_left"][0] try: print('./calibration_data/{}p/camera_calibration{}.npz'.format( res_y, right_or_left)) npz_file = np.load( './calibration_data/{}p/camera_calibration{}.npz'.format( res_y, right_or_left)) list_of_vars = [ 'map1', 'map2', 'objpoints', 'imgpoints', 'camera_matrix', 'distortion_coeff' ] print(sorted(npz_file.files)) if sorted(list_of_vars) == sorted(npz_file.files): print("Camera calibration data has been found in cache.") map1 = npz_file['map1'] map2 = npz_file['map2'] objectPoints = npz_file['objpoints'] if right_or_left == "_right": rightImagePoints = npz_file['imgpoints'] rightCameraMatrix = npz_file['camera_matrix'] rightDistortionCoefficients = npz_file['distortion_coeff'] if right_or_left == "_left": leftImagePoints = npz_file['imgpoints'] leftCameraMatrix = npz_file['camera_matrix'] leftDistortionCoefficients = npz_file['distortion_coeff'] else: print("Camera data file found but data corrupted.") except: #If the file doesn't exist print("Camera calibration data not found in cache.") return False print("Calibrating cameras together...") leftImagePoints = np.asarray(leftImagePoints, dtype=np.float64) rightImagePoints = np.asarray(rightImagePoints, dtype=np.float64) # Stereo calibration (RMS, _, _, _, _, rotationMatrix, translationVector) = cv2.fisheye.stereoCalibrate( objectPoints, leftImagePoints, rightImagePoints, leftCameraMatrix, leftDistortionCoefficients, rightCameraMatrix, rightDistortionCoefficients, imageSize, None, None, cv2.CALIB_FIX_INTRINSIC, TERMINATION_CRITERIA) # Print RMS result (for calibration quality estimation) print("<><><><><><><><><><><><><><><><><><><><>") print("<><> RMS is ", RMS, " <><>") print("<><><><><><><><><><><><><><><><><><><><>") print("Rectifying cameras...") R1 = np.zeros([3, 3]) R2 = np.zeros([3, 3]) P1 = np.zeros([3, 4]) P2 = np.zeros([3, 4]) Q = np.zeros([4, 4]) # Rectify calibration results (leftRectification, rightRectification, leftProjection, rightProjection, dispartityToDepthMap) = cv2.fisheye.stereoRectify( leftCameraMatrix, leftDistortionCoefficients, rightCameraMatrix, rightDistortionCoefficients, imageSize, rotationMatrix, translationVector, 0, R2, P1, P2, Q, cv2.CALIB_ZERO_DISPARITY, (0, 0), 0, 0) # Saving calibration results for the future use print("Saving calibration...") leftMapX, leftMapY = cv2.fisheye.initUndistortRectifyMap( leftCameraMatrix, leftDistortionCoefficients, leftRectification, leftProjection, imageSize, cv2.CV_16SC2) rightMapX, rightMapY = cv2.fisheye.initUndistortRectifyMap( rightCameraMatrix, rightDistortionCoefficients, rightRectification, rightProjection, imageSize, cv2.CV_16SC2) np.savez_compressed( './calibration_data/{}p/stereo_camera_calibration.npz'.format(res_y), imageSize=imageSize, leftMapX=leftMapX, leftMapY=leftMapY, rightMapX=rightMapX, rightMapY=rightMapY, dispartityToDepthMap=dispartityToDepthMap) return True
def run(self): # Capture, process, display loop while True: # Read a new frame ok, self.frame = self.video.read() w, h, ch = self.frame.shape self.frame = cv2.resize(self.frame, (int(h/2), int(w/2)), interpolation=cv2.INTER_NEAREST) display = self.frame.copy() # Frame we'll do all the graphical drawing to data_display = np.zeros_like(display, dtype=np.uint8) # Black screen to display data if not ok: break # Start FPS timer timer = cv2.getTickCount() if self.bg is None: cv2.putText(display, "Press 'r' to reset the foreground extraction.", self.positions['hand_pose'], cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 127, 64), 2) cv2.imshow("display", display) k = cv2.waitKey(1) & 0xff if k == 27: break # ESC pressed elif k == 114 or k == 108: # r pressed self.bg = self.frame.copy() self.hand_bbox = (116, 116, 170, 170) self.is_tracking = False else: # Extract the foreground foreground, mask = self.extract_foreground() foreground_display = foreground.copy() # Get hand from mask using the bounding box hand_crop = mask[int(self.hand_bbox[1]):int(self.hand_bbox[1]+self.hand_bbox[3]), int(self.hand_bbox[0]):int(self.hand_bbox[0]+self.hand_bbox[2])] # Update tracker if self.is_tracking: tracking, self.hand_bbox = self.tracker.update(foreground) try: # Resize cropped hand and make prediction on gesture hand_crop_resized = np.expand_dims(cv2.resize(hand_crop, (54, 54)), axis=0).reshape((1, 54, 54, 1)) prediction = self.recognizer.predict(hand_crop_resized) predi = prediction[0].argmax() # Get the index of the greatest confidence gesture = self.CLASSES[predi] for i, pred in enumerate(prediction[0]): # Draw confidence bar for each gesture barx = self.positions['hand_pose'][0] bary = 60 + i*60 bar_height = 20 bar_length = int(400 * pred) + barx # calculate length of confidence bar # Make the most confidence prediction green if i == predi: colour = (0, 255, 0) else: colour = (0, 0, 255) cv2.putText(data_display, "{}: {}".format(self.CLASSES[i], pred), (self.positions['hand_pose'][0], 30 + i*60), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 255), 2) cv2.rectangle(data_display, (barx, bary), (bar_length, bary - bar_height), colour, -1, 1) cv2.putText(display, "hand pose: {}".format(gesture), self.positions['hand_pose'], cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) cv2.putText(foreground_display, "hand pose: {}".format(gesture), self.positions['hand_pose'], cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) except Exception as ex: cv2.putText(display, "hand pose: error", self.positions['hand_pose'], cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) cv2.putText(foreground_display, "hand pose: error", self.positions['hand_pose'], cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) # Draw bounding box p1 = (int(self.hand_bbox[0]), int(self.hand_bbox[1])) p2 = (int(self.hand_bbox[0] + self.hand_bbox[2]), int(self.hand_bbox[1] + self.hand_bbox[3])) cv2.rectangle(foreground_display, p1, p2, (255, 0, 0), 2, 1) cv2.rectangle(display, p1, p2, (255, 0, 0), 2, 1) # Calculate difference in hand position hand_pos = ((p1[0] + p2[0])//2, (p1[1] + p2[1])//2) mouse_change = ((p1[0] + p2[0])//2 - self.positions['null_pos'][0], self.positions['null_pos'][0] - (p1[1] + p2[1])//2) # Draw hand moved difference cv2.circle(display, self.positions['null_pos'], 5, (0,0,255), -1) cv2.circle(display, hand_pos, 5, (0,255,0), -1) cv2.line(display, self.positions['null_pos'], hand_pos, (255,0,0),5) # Calculate Frames per second (FPS) fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer) # Display FPS on frame cv2.putText(foreground_display, "FPS : " + str(int(fps)), self.positions['fps'], cv2.FONT_HERSHEY_SIMPLEX, 0.65, (50, 170, 50), 2) cv2.putText(display, "FPS : " + str(int(fps)), self.positions['fps'], cv2.FONT_HERSHEY_SIMPLEX, 0.65, (50, 170, 50), 2) # Display pause command text cv2.putText(foreground_display, "hold 'r' to recalibrate until the screen is black", (15, 400), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2) cv2.putText(foreground_display, "to recalibrate", (15, 420), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2) cv2.putText(foreground_display, "press 'p' to return to paused state", (15, 450), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2) # Display foreground_display cv2.imshow("foreground_display", foreground_display) cv2.imshow("display", display) cv2.imshow("hand_crop", hand_crop) # Display result cv2.imshow("data", data_display) k = cv2.waitKey(1) & 0xff if k == 27: break # ESC pressed elif k == 114 or k == 108: # r pressed self.bg = self.frame.copy() self.hand_bbox = (116, 116, 170, 170) self.is_tracking = False elif k == 116: # t pressed # Initialize tracker with first frame and bounding box self.is_tracking = True self.tracker = cv2.TrackerKCF_create() self.tracker.init(self.frame, self.hand_bbox) elif k == 112: # p pressed # Reset to paused state self.is_tracking = False self.bg = None cv2.destroyAllWindows() elif k != 255: print(k)
desc = stack_array([img_data.bow_descriptors for img_data in imgs_data]) desc = np.float32(desc) # clustering criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, params.BOW_clustering_iterations, 0.01) flags = cv2.KMEANS_PP_CENTERS compactness, labels, dictionary = cv2.kmeans(desc, dictionary_size, None, criteria, 1, flags) np.save(params.BOW_DICT_PATH, dictionary) return dictionary program_start = cv2.getTickCount() # get training set print("Loading images...") start = cv2.getTickCount() # must specify data path names in same order as class names paths = [params.DATA_training_path_neg, params.DATA_training_path_pos] # get class names class_names = [ get_class_name(class_number) for class_number in range(len(params.DATA_CLASS_NAMES)) ] # again, must specify in same order as class names sampling_sizes = [ params.DATA_training_sample_count_neg, params.DATA_training_sample_count_pos
''' 3. Performance-measurement and -improvement ''' import cv2 import numpy as np import matplotlib.pyplot as plt # The modules time and profile from python are assisting well on this behalf # cv2.getTickCount() is like tic() toc() in other languages e1 = cv2.getTickCount() # Is the same as time.time() img = cv2.imread('test_image.png') plt.imshow(img[:,:,::1]) plt.show() e2 = cv2.getTickCount() # cv2.getTickFrequency = numbers of clock-cycles per second t = (e2 - e1) / cv2.getTickFrequency() print("e2 - e1 = {}\n".format(e2 - e1)) print("t = {}".format(t)) # OpenCV has much optimized code by default: cv2.setUseOptimized(True) cv2.useOptimized() %timeit res = cv2.medianBlur(img,49) # With disabled optimization cv2.setUseOptimized(False) cv2.useOptimized() %timeit res = cv2.medianBlur(img,49)
import cv2 as cv import numpy as np # 왜 최적화가 필요한가? 이미지처리에는 엄청나게 많은 연산이 진행되기 때문 # 배울 것 # cv.getTickCount # cv.getTickFrequency img = cv.imread("../1_arithmetic_operation/apple.jpg") e1 = cv.getTickCount() # 코드 실행 시간 for i in range(5, 49, 2): img = cv.medianBlur(img, i) e2 = cv.getTickCount() # 코드 실행 종료 시간 t = (e2 - e1) * 1000 / cv.getTickFrequency() # 총 코드 실행 시간 print(t, "ms")
def getCMarkers (img): e1 = cv2.getTickCount() markers = [] gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img3 = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 15, 10) kernel = np.ones((2,2),np.uint8) img2 = cv2.morphologyEx(img3, cv2.MORPH_CLOSE, kernel) kernel = np.ones((2,2),np.uint8) img2 = cv2.dilate(img2,kernel, 1) # cv2.imshow("test1", img2) # Setup SimpleBlobDetector parameters. params = cv2.SimpleBlobDetector_Params() params.filterByInertia = False params.filterByConvexity = False # Filter by Area. params.filterByArea = True params.minArea = 5 params.minDistBetweenBlobs = 1 # Filter by Circularity params.filterByCircularity = True params.minCircularity = 0.5 # # Filter by Convexity params.filterByConvexity = True params.minConvexity = 0.70 # Filter by Inertia params.filterByInertia = True params.minInertiaRatio = 0.2 params.filterByColor = False # Create a detector with the parameters detector = cv2.SimpleBlobDetector_create(params) # Detect blobs. keypoints = detector.detect(img2) points = [] validpoints = [] for point in keypoints: count = [] for p in keypoints: distance = (p.pt[0]-point.pt[0])**2+(p.pt[1]-point.pt[1])**2 if p == point: continue elif distance <= (point.size*4.5)**2 and (abs(point.size/p.size-1) <= 0.3): count.append(p.pt) if len(count) >= 2: points.append((point.pt, count)) validpoints.append(point) for point in points: p, near = point # distance open the angre and 90 degree midistance = math.pi/30.0 bottom = [] rigth = [] for p1 in near: for p2 in near: if p1 == p2: continue u = np.array([p1[0]-p[0], p1[1]-p[1]]) v = np.array([p2[0]-p[0], p2[1]-p[1]]) angle = np.math.atan2(np.linalg.det([u,v]),np.dot(u,v)) if abs(angle-math.pi/2.0) < math.pi/30.0: bottom = p1 rigth = p2 conner = rigth+u addu = u/6.0 addv = v/6.0 conners = [p-addu-addv, bottom+addu-addv, rigth-addu+addv, conner+addu+addv] trans = transformMatrixPoints(conners, [10, 10], 10) code = cv2.warpPerspective(gray, trans, dsize=(100, 100)) # code = cv2.adaptiveThreshold(code, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 25, 1) number = getNumber(code, 160) if number == False: continue # cv2.imshow("m2", code) uu = np.array([0, 1]) angle = np.math.atan2(np.linalg.det([v,uu]),np.dot(v,uu)) mid = p+u*0.5+v*0.5 if number != 0: markers.append([number, mid, angle]) # img2 = cv2.drawKeypoints(img2, validpoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) # cv2.imshow("m3", img2) return markers
bbox = (287, 23, 86, 320) # Uncomment the line below to select a different bounding box bbox = cv2.selectROI(frame, False) # Initialize tracker with first frame and bounding box ok = tracker.init(frame, bbox) while True: # Read a new frame ok, frame = video.read() if not ok: break # Start timer timer = cv2.getTickCount() # Update tracker ok, bbox = tracker.update(frame) #fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer); # Draw bounding box if ok: # Tracking success p1 = (int(bbox[0]), int(bbox[1])) p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3])) cv2.rectangle(frame, p1, p2, (255, 0, 0), 2, 1) else: # Tracking failure cv2.putText(frame, "Tracking failure detected", (100, 80),
def cascade_vh(self): face_flag = 0 target_lose_cnt = 0 while (cv2.waitKey(1) != 27): start_tc = cv2.getTickCount() grabbed, frame = self.cap.read() if not grabbed: logging.info("READ OVER") break if not face_flag: face_bbox = self.detector.face_detection(frame) # No face has been detected if isinstance(face_bbox, int): cv2.imshow("frame", frame) continue # detect successfully else: logging.info("tracker init") face_flag = 1 face_bbox = self.expand_bbox(*face_bbox) self.tracker.start_track(frame, *face_bbox) target_lose_cnt = 0 else: # track if target_lose_cnt < self.lose_threshold: (success, box_predict) = self.tracker.update_track(frame) if not success: logging.debug("update failed") target_lose_cnt += 1 cv2.imshow("frame", frame) continue (old_x, old_y, old_w, old_h) = (int(v) for v in box_predict) # draw predict rect # self.draw_rect(frame,(old_x, old_y, old_w, old_h),(255,0,0)) # face_bbox is relative coordinates!!!! face_bbox = self.detector.face_detection( frame[old_y:old_y + old_h, old_x:old_x + old_w]) if isinstance(face_bbox, int): target_lose_cnt += 1 cv2.imshow("frame", frame) continue target_lose_cnt = 0 face_bbox = self.expand_bbox(*face_bbox) face_bbox[0] += int(old_x) face_bbox[1] += int(old_y) # lose target else: logging.info("losing target") # reset tracker self.tracker = Tracker() face_flag = 0 cv2.imshow("frame", frame) continue # self.draw_rect(frame,face_bbox,(0,255,0)) face_rect = self.bbox_to_rect(face_bbox) success, frame = self.run_face_swap(frame, face_rect) if not success: pass cv2.imshow("output", frame) end_tc = cv2.getTickCount() fps = cv2.getTickFrequency() / (end_tc - start_tc) logging.info("fps {}".format(fps)) cv2.destroyAllWindows() self.cap.release()
# # frame = cv.pyrMeanShiftFiltering(frame, 20, 100) # frame = cv.GaussianBlur(frame, (3, 3), 0) # # hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV) # # mask1 = cv.inRange(hsv, lowerb=lower_hsv, upperb=upper_hsv) # mask = cv.inRange(frame, lowerb=np.array([0, 0, 110]), upperb=np.array([110, 110, 255])) # kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3)) # mask = cv.morphologyEx(mask, cv.MORPH_OPEN, kernel) # # mask = cv.bitwise_or(mask1, mask2) # # dst = cv.bitwise_and(frame, frame, mask=mask) # contours, heriachy = cv.findContours(mask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) # for i, contour in enumerate(contours): # contour_area = cv.contourArea(contour) # x, y, w, h = cv.boundingRect(contour) # cv.drawContours(mask, contours, i, (0, 0, 255), 3) # if contour_area > 0.8 * w * h: # cv.drawContours(frame, contours, i, (255, 0, 255), 2) # cv.imshow("mask", mask) # cv.imshow("video", frame) # if cv.waitKey(40) == 27: # break t1 = cv.getTickCount() extrace_object_demo() t2 = cv.getTickCount() print("time: %s ms" % ((t2 - t1) / cv.getTickFrequency() * 1000)) # cv.destroyAllWindows()
from __future__ import print_function import cv2 as cv capture = cv.VideoCapture(0) ret, frame_first = capture.read() ret, frame_first = capture.read() ret, frame_first = capture.read() ret, frame_first = capture.read() ret, frame_first = capture.read() frame_first_gray = cv.cvtColor(frame_first, cv.COLOR_BGR2GRAY) frame_first_blurred = cv.GaussianBlur(frame_first_gray, (5, 5), 0) while True: start = cv.getTickCount() _, frame = capture.read() if frame is None: break frame_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) frame_blurred = cv.GaussianBlur(frame_gray, (5, 5), 0) # frame_canny = cv.Canny(frame_blurred, 50, 150) difference = cv.absdiff(frame_first_blurred, frame_blurred) _, difference = cv.threshold(difference, 25, 255, cv.THRESH_BINARY) difference_blurred = cv.GaussianBlur(difference, (5, 5), 0) cv.imshow('BG Filtered', difference_blurred) keyboard = cv.waitKey(30) if keyboard == 'q' or keyboard == 27:
while True: ok, frame = video.read() if not ok: break cv.imshow('Tracking', frame) if cv.waitKey(1) != -1: break bbox = cv.selectROI('Tracking', frame, showCrosshair=False) print(bbox) # Initialize tracker with first frame and bounding box ok = tracker.init(frame, bbox) print('tracker.init :', ok) count = 0 fps = 0 t0 = cv.getTickCount() while True: ok, frame = video.read() if not ok: break if cv.waitKey(1) == 27: break # Update tracker ok, bbox = tracker.update(frame) # Draw bounding box if ok: # Tracking success p1 = (int(bbox[0]), int(bbox[1])) p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
if __name__ == '__main__': parser = argparse.ArgumentParser(description='Human face detect and Track') parser.add_argument('--video_path', default=0, help='Path for video') args = parser.parse_args() video_path = args.parse_args() d = Detector() t = Tracker() cap = cv2.VideoCapture(video_path) face_flag = 0 lose_threshold = 10 target_lose_cnt = 0 while (cv2.waitKey(1) != 27): start_tc = cv2.getTickCount() grabbed, frame = cap.read() if not grabbed: break if not face_flag: face_bbox = d.face_detection(frame) # No face has been detected if isinstance(face_bbox, int): cv2.imshow("output", frame) continue # detect successfully else: logging.info("tracker init") face_flag = 1 face_bbox = expand_bbox(*face_bbox)
SIZE = h_img.size NUM_BIN = 256 n_threads = int(numpy.ceil((SIZE + NUM_BIN - 1) / NUM_BIN)) start = drv.Event() end = drv.Event() start.record() # Calling kernel atomic_hist(drv.Out(h_result), drv.In(h_a), numpy.uint32(SIZE), block=(n_threads, 1, 1), grid=(NUM_BIN, 1)) end.record() end.synchronize() secs = start.time_till(end) * 1e-3 print("Time for Calculating Histogram on GPU without shared memory") print("%fs" % (secs)) plt.stem(h_result) plt.xlim([0, 256]) plt.title("Histogram on GPU") start = cv2.getTickCount() # OpenCV calcHist hist = cv2.calcHist([h_img], [0], None, [256], [0, 256]) end = cv2.getTickCount() secs = (end - start) / cv2.getTickFrequency() print("Time for Calculating Histogram using OpenCV") print("%fs" % (secs)) # plt.stem(hist) # plt.xlim([0,256])
def main(model_code): # create tracker tracker_info = Tracker(args.tracker_name, args.tracker_param, None) params = tracker_info.get_parameters() params.visualization = args.vis params.debug = args.debug params.visdom_info = { 'use_visdom': False, 'server': '127.0.0.1', 'port': 8097 } tracker = tracker_info.tracker_class(params) RF_module = RefineModule(refine_path.format(model_code), selector_path, search_factor=sr, input_sz=input_sz) model_name = args.tracker_name + '_' + args.tracker_param +\ '{}-{}'.format(RF_type.format(model_code), selector_path) + '_%d'%(args.run_id) # create dataset dataset = DatasetFactory.create_dataset(name=args.dataset, dataset_root=dataset_root_, load_img=False) if args.dataset in ['VOT2016', 'VOT2018', 'VOT2019']: total_lost = 0 # restart tracking for v_idx, video in enumerate(dataset): if args.video != '': # test one special video if video.name != args.video: continue frame_counter = 0 lost_number = 0 toc = 0 '''对refinement module计时''' toc_refine = 0 pred_bboxes = [] for idx, (img, gt_bbox) in enumerate(video): if len(gt_bbox) == 4: gt_bbox = [ gt_bbox[0], gt_bbox[1], gt_bbox[0], gt_bbox[1] + gt_bbox[3] - 1, gt_bbox[0] + gt_bbox[2] - 1, gt_bbox[1] + gt_bbox[3] - 1, gt_bbox[0] + gt_bbox[2] - 1, gt_bbox[1] ] tic = cv2.getTickCount() '''get RGB format image''' img_RGB = img[:, :, ::-1].copy() # BGR --> RGB if idx == frame_counter: H, W, _ = img.shape cx, cy, w, h = get_axis_aligned_bbox(np.array(gt_bbox)) gt_bbox_ = [cx - (w - 1) / 2, cy - (h - 1) / 2, w, h] '''Initialize''' gt_bbox_np = np.array(gt_bbox_) gt_bbox_torch = torch.from_numpy( gt_bbox_np.astype(np.float32)) init_info = {} init_info['init_bbox'] = gt_bbox_torch _ = tracker.initialize(img_RGB, init_info) '''##### initilize refinement module for specific video''' RF_module.initialize(img_RGB, np.array(gt_bbox_)) pred_bbox = gt_bbox_ pred_bboxes.append(1) elif idx > frame_counter: '''Track''' outputs = tracker.track(img_RGB) pred_bbox = outputs['target_bbox'] '''##### refine tracking results #####''' pred_bbox = RF_module.refine(img_RGB, np.array(pred_bbox)) '''report result and update state''' x1, y1, w, h = pred_bbox.tolist() '''add boundary and min size limit''' x1, y1, x2, y2 = bbox_clip(x1, y1, x1 + w, y1 + h, (H, W)) w = x2 - x1 h = y2 - y1 new_pos = torch.from_numpy( np.array([y1 + h / 2, x1 + w / 2]).astype(np.float32)) new_target_sz = torch.from_numpy( np.array([h, w]).astype(np.float32)) new_scale = torch.sqrt(new_target_sz.prod() / tracker.base_target_sz.prod()) ##### update tracker.pos = new_pos.clone() tracker.target_sz = new_target_sz tracker.target_scale = new_scale overlap = vot_overlap(pred_bbox, gt_bbox, (img.shape[1], img.shape[0])) if overlap > 0: # not lost pred_bboxes.append(pred_bbox) else: # lost object pred_bboxes.append(2) frame_counter = idx + 5 # skip 5 frames lost_number += 1 else: pred_bboxes.append(0) toc += cv2.getTickCount() - tic if idx == 0: cv2.destroyAllWindows() if args.vis and idx > frame_counter: cv2.polylines( img, [np.array(gt_bbox, np.int).reshape( (-1, 1, 2))], True, (0, 255, 0), 3) if len(pred_bbox) == 8: cv2.polylines( img, [np.array(pred_bbox, np.int).reshape( (-1, 1, 2))], True, (0, 255, 255), 3) else: bbox = list(map(int, pred_bbox)) cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[0] + bbox[2], bbox[1] + bbox[3]), (0, 255, 255), 3) cv2.putText(img, str(idx), (40, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2) cv2.putText(img, str(lost_number), (40, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) cv2.imshow(video.name, img) cv2.waitKey(1) toc /= cv2.getTickFrequency() # save results video_path = os.path.join(save_dir, args.dataset, model_name, 'baseline', video.name) if not os.path.isdir(video_path): os.makedirs(video_path) result_path = os.path.join(video_path, '{}_001.txt'.format(video.name)) with open(result_path, 'w') as f: for x in pred_bboxes: if isinstance(x, int): f.write("{:d}\n".format(x)) else: f.write(','.join([vot_float2str("%.4f", i) for i in x]) + '\n') print( '({:3d}) Video: {:12s} Time: {:4.1f}s Speed: {:3.1f}fps Lost: {:d}' .format(v_idx + 1, video.name, toc, idx / toc, lost_number)) total_lost += lost_number print("{:s} total lost: {:d}".format(model_name, total_lost))
import cv2 face_cascade = cv2.CascadeClassifier("./haarcascade_frontalface_alt2.xml") cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() tickmark = cv2.getTickCount() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) face = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=3) for x, y, w, h in face: cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) if cv2.waitKey(1) & 0xFF == ord('q'): break fps = cv2.getTickFrequency() / (cv2.getTickCount() - tickmark) cv2.putText(frame, "FPS: {:05.2f}".format(fps), (10, 30), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 2) cv2.imshow('video', frame) cap.release() cv2.destroyAllWindows()
import cv2 cv2.setUseOptimized(True) e1 = cv2.getTickCount() img1 = cv2.imread('capture 0.png') img2 = cv2.imread('images.png') rows, cols, channels = img2.shape roi = img1[0:rows, 0:cols] img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) ret, mask = cv2.threshold(img2gray, 1, 10, cv2.THRESH_BINARY) mask_inv = cv2.bitwise_not(mask) img1_bg = cv2.bitwise_and(roi, roi, mask=mask_inv) img2_fg = cv2.bitwise_and(img2, img2, mask=mask) dst = cv2.add(img1_bg, img2_fg) img1[0:rows, 0:cols] = dst cv2.imshow('image', img1) e2 = cv2.getTickCount() t = (e2 - e1) / cv2.getTickFrequency() print t cv2.waitKey(0)
def debug(msg): if Debug.debug_mode: print("%f: %s" % ( cv.getTickCount(), msg ))
# programa para calcular frame fps en varios escenarios # 1) basico sin sin ninguna programacion especial. # prom 48 fps, min 34, max 68. import cv2 captura = cv2.VideoCapture(0) while (captura.isOpened()): timer = cv2.getTickCount() # inicia el tiempo de fps ret, imagen = captura.read() if ret == True: fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer ) # calcula el tiempo fps # voltea la imagen imagen = cv2.flip(imagen, 0) cv2.putText(imagen, str(int(fps)), (75, 50), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0, 255, 0), 2) cv2.imshow('video', imagen) #print("Frames por segundos: ", str(int(fps))) if cv2.waitKey(1) & 0xFF == ord('s'): break else: break captura.release() cv2.destroyAllWindows()
return data, rows, cols, dims train_data, rows, cols, dims = image_read( 'D:\\Learning materials\\machine_homework\\training4' ) # 载入对应的训练图片,共20个训练样本 test_data = cv.imread( 'D:\\Learning materials\\machine_homework\\test3\\image21.jpg' ) # 载入对应的测试图片,有一个测试样本 test_data = np.asarray(test_data) # 将图片转化成数组形式 h, temp = 100, 1 # 带宽值设定为120,中间变量为1 P = np.zeros((rows, cols)) # 一个二维的全0数组,为最后的输出概率 P1 = np.empty((rows, cols, dims)) # 三维的全空数组 P2 = np.zeros((rows, cols)) # 二维的全0数组 t1 = cv.getTickCount() # 计算整个kde算法所需要的时间 for i in range(19): # 共19个训练样本 for j in range(rows): for k in range(cols): for l in range(dims): P1[j, k, l] = 1 - pow( (test_data[j, k, l] - train_data[j, k, l, i]) / h, 2) if P1[j, k, l] < 0: # 如果概率密度函数值小于0,那么将其赋值为0 P1[j, k, l] = 0 temp = temp * P1[j, k, l] P2[j, k] = temp temp = 1.0 P = P + P2 # 概率值叠加 P = 15 / (8 * pi * 20 * h**3) * P # EP函数的表达式,其中N=20