예제 #1
0
    def reDetect( self, use_camshift = True, expand_pixels = 1 ):
        if self.frame_idx % self.detect_interval == 0 and len(self.tracks) > 0:

            if use_camshift:
                mask_camshift = np.zeros_like( self.frame_gray )
                ellipse_camshift = self.getCamShift()
                cv2.ellipse( mask_camshift, ellipse_camshift, 255, -1 )

            mask_tracked = np.zeros_like( self.frame_gray )
            pts = np.int32( [ [tr[-1]] for tr in self.tracks ] )

            if len(pts) > 5:
                ellipse_tracked = cv2.fitEllipse( pts )
            else:
                ellipse_tracked = cv2.minAreaRect( pts )

            boundingRect = cv2.boundingRect( pts )
            # x0,y0,x1,y1 = ( boundingRect[0] - int(boundingRect[2] * 0.05),
            #                 boundingRect[1] - int(boundingRect[3] * 0.05),
            #                 boundingRect[0] + int(boundingRect[2] * 1.05),
            #                 boundingRect[1] + int(boundingRect[3] * 1.05))


            x0,y0,x1,y1 = ( boundingRect[0] - expand_pixels,
                            boundingRect[1] - expand_pixels,
                            boundingRect[0] + boundingRect[2] + expand_pixels,
                            boundingRect[1] + boundingRect[3] + expand_pixels )
            
            if len( pts ) > 2:
                cv2.rectangle( mask_tracked, (x0,y0), (x1,y1), 255, -1 )

            cv2.ellipse( mask_tracked, ellipse_tracked, 255, -1 )

#            cv2.imshow( 'mask_camshift', mask_camshift )
#            cv2.imshow( 'mask_tracked', mask_tracked )

            if use_camshift:
                mask = mask_camshift & mask_tracked
            else:
                mask = mask_tracked

            for x, y in [np.int32(tr[-1]) for tr in self.tracks]:
                cv2.circle(mask, (x,y), 5, 0, -1 )

#            cv2.imshow( 'mask', mask )
            start = cv2.getTickCount() / cv2.getTickFrequency()
            p = cv2.goodFeaturesToTrack( self.frame_gray, mask = mask, **feature_params )
            self.timers['GoodFeatures'] = cv2.getTickCount() / cv2.getTickFrequency() - start

            if p is not None:
                for x, y in np.float32(p).reshape(-1, 2):
                    self.tracks.append([(x,y)])
예제 #2
0
    def pickFeatures(self):
        mask = np.zeros_like( self.frame_gray )
#        import pdb; pdb.set_trace()

        cv2.rectangle( mask, tuple( self.userRect[0] ), tuple( self.userRect[1] ), 255, -1 )
#        cv2.imshow( 'userMask', mask )

        start = cv2.getTickCount() / cv2.getTickFrequency()
        p = cv2.goodFeaturesToTrack( self.frame_gray, mask = mask, **feature_params )
        self.timers['GoodFeatures'] = cv2.getTickCount() / cv2.getTickFrequency() - start

        if p is not None:
            self.tracks = [ [ (x,y) ] for x, y in np.float32(p).reshape(-1, 2) ]
예제 #3
0
    def __init__(self, action, video=None, background=None, verbose=True):
        # action is a function object
        # for interframe processing: action: frame -> frame
        self.action = action

        self.verbose = verbose

        self.comm, self.rank, self.np = mpi.get_mpi_info()

        if self.np == 0:
            raise ZeroDivisionError

        self.comm_times = 0.0
        self.comp_times = 0.0

        self.log("Tick frequency: {0}".format(getTickFrequency()))

        if getTickFrequency() == 0:
            raise ZeroDivisionError

        if self.rank == 0:
            if video:
                # get from object constructor
                # should be only for testing
                self.vid = video
            else:
                # get from command line arguments
                self.vid = tools.init()

            # each process gets clip_size/np frames to operate on
            self.frames_per_block = self.vid.clip_size / self.np

            # load background image from video
            self.background = self.vid.background

            # tell children what size frame block to expect
            self.send_to_children(self.frames_per_block, tag=mpi.VIDEO_INIT)

            # tell children what the background image is
            self.send_to_children(self.background, tag=mpi.BACKGROUND)

            self.log("Clip size: {0} frames\n{1} processes\nFrames per process: {2}".format(self.vid.clip_size, self.np, self.frames_per_block))
        else:
            self.frames_per_block = self.recv_from_parent(mpi.VIDEO_INIT)
            self.background = self.recv_from_parent(mpi.BACKGROUND)

        # load face cascade in case experiment
        # will go to None if not present
        self.cascade = tools.load_cascade()
예제 #4
0
파일: tcor.py 프로젝트: y-iikura/git_python
def mclass2(cls1,cls2,cls3):
    cls1x=np.float32(cls1.reshape(1440000))
    cls2x=np.float32(cls2.reshape(1440000))
    cls3x=np.float32(cls3.reshape(1440000))
    data=np.array([[cls1x],[cls2x],[cls3x]])
    data=data.reshape(3,1440000)
    data=np.transpose(data)
    datax=data[::100,:]
    t=cv2.getTickCount()
    codebook, destortion = scipy.cluster.vq.kmeans(datax, 2560, iter=10, thresh=1e-05)
    print (cv2.getTickCount()-t)/cv2.getTickFrequency()
    t=cv2.getTickCount()
    code, dist = scipy.cluster.vq.vq(data, codebook)
    print (cv2.getTickCount()-t)/cv2.getTickFrequency()
    return code.reshape(1200,1200)
    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
예제 #6
0
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()
예제 #7
0
    def run(self):
        """Runs the worm tracking algorithm indefinitely"""

        for i in range(100):
            #Spool off 100 images to allow camera to auto-adjust
            self.img = self.camera.read()

        while True:

            self.read_trackbars()
            ret, self.img = self.camera.read()
            self.find_worm()

            if self.skeleton:
                self.skeletonise()

            self.update_gui()
            self.move_stage()

            #FPS calculations
            now = cv2.getTickCount()
            fps = cv2.getTickFrequency()/(now - self.last_frame)
            self.last_frame = now

            print fps
예제 #8
0
def test_strategy_intercept_calc():
    pred = strategy.PyMunkPredictor(WIDTH, HEIGHT)

    time_step = cv2.getTickFrequency() * 0.1
    # [ puck-position, expected_intercept_point ]
    COORDS = [
        ((160,120), None),
        ((180,120), (280.0, 120.0)),
        ((200,120), (280.0, 120.0)),
        ((210,120), (280.0, 120.0)),
        ((220,120), (280.0, 120.0)),
        ((240,120), (280.0, 120.0)),
        ((260,120), (280.1, 120.0)),
        ((280,120), None),
        ((300,120), None),
        ((320,120), None)
        ]

    for (step, (pos, expected)) in zip(range(10), COORDS):
        print step, pos
        sim_time = time_step * step
        pred.add_puck_event(sim_time, pos, PUCK_RADIUS)
        i_point = pred.intercept_point()
        if i_point is None or expected is None:
            assert i_point == expected
        else:
            np.allclose(i_point, expected)
예제 #9
0
파일: detector.py 프로젝트: hphp/Kaggle
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 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()
예제 #11
0
def mclass(cls1,cls2,cls3,nmax):
    num=imax*jmax
    cls1x=np.float32(cls1.reshape(num))
    cls2x=np.float32(cls2.reshape(num))
    cls3x=np.float32(cls3.reshape(num))
    data=np.array([[cls1x],[cls2x],[cls3x]])
    data=data.reshape(3,num)
    data=np.transpose(data)
    datax=data[::100,:]
    t=cv2.getTickCount()
    codebook, destortion = scipy.cluster.vq.kmeans(datax, nmax, iter=10, thresh=1e-05)
    print (cv2.getTickCount()-t)/cv2.getTickFrequency()
    t=cv2.getTickCount()
    code, dist = scipy.cluster.vq.vq(data, codebook)
    print (cv2.getTickCount()-t)/cv2.getTickFrequency()
    return code.reshape(jmax,imax)
예제 #12
0
    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
예제 #13
0
파일: GUI.py 프로젝트: jabreu4/TakeHome2
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()
예제 #14
0
 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()
예제 #15
0
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 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
예제 #17
0
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]
예제 #19
0
    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))
예제 #20
0
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
예제 #21
0
파일: MLP.py 프로젝트: kelvict/rnn-py
 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          
예제 #22
0
파일: utils.py 프로젝트: Kieleth/behave
 def __init__(self):
     self.tick_frequency = cv2.getTickFrequency()
     self.tick_at_init = cv2.getTickCount()
     self.last_tick = self.tick_at_init
     self.fps_len = 100
     self.l_fps_history = [ 10 for x in range(self.fps_len)]
     self.fps_counter = circular_counter(self.fps_len)
     self.frame_num = 0
예제 #23
0
    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()
예제 #24
0
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 ''
예제 #25
0
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;
예제 #26
0
    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()
예제 #27
0
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
예제 #28
0
파일: tcor.py 프로젝트: y-iikura/git_python
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)
예제 #29
0
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 run(self):
        # This method runs in a separate thread
        global done, frame, time, execTime
        while not self.terminated:
            # Wait for an image to be written to the stream
            if self.event.wait(1):
                try:
                    e1 = cv2.getTickCount()
                    self.stream.seek(0)
                    # Read the image and do some processing on it

                    #filt.image = self.stream.array
                    data = np.fromstring(self.stream.getvalue(), dtype=np.uint8)
                    filt.image = cv2.imdecode(data, 1)
                    # ABOVE IS EXPENSIVE - FIND AN ALTERNATIVE

                    filtered, imagey, contours, h = filt.rgbGet(cv2.CHAIN_APPROX_SIMPLE, Constants.VIDEOS_RGB_FILTER_CONSTANTS_1)
                    coolImage = filt.run(filt.image)
                    # Set done to True if you want the script to terminate
                    # at some point
                    #done=True
                    self.stream.truncate()
                    e2 = cv2.getTickCount()
                    execTime = (e2 - e1) / cv2.getTickFrequency()
                    time += execTime
                    frame += 1
                    if frame >= numFrames:
                        done = True
                    print execTime
                # except:
                #     pass
                finally:
                    # Reset the stream and event
                    self.stream.seek(0)
                    self.stream.truncate()
                    self.event.clear()
                    e2 = cv2.getTickCount()
                    time += (e2 - e1) / cv2.getTickFrequency()
                    frame += 1
                    # Return ourselves to the pool
                    with lock:
                        pool.append(self)
예제 #31
0
    def streaming(self):

        try:
            print("Host: ", self.host_name + ' ' + self.host_ip)
            print("Connection from: ", self.connecting_address)
            print("Streaming...")
            print("Press 'q' to exit")

            print('Send first control signal')
            self.connection.write('01'.encode())  # move forward
            self.connection.flush()
            # self.send_socket.send('go'.encode())

            signal = 1
            old_signal = -1

            request_num_customers = False
            stop_flag = False
            stop_sign_active = True

            frame_num = 0

            stream_bytes = b' '
            received = []

            while True:
                frame_num += 1

                # controlCommand = '-1'  # do nothing

                # print('Get byte')
                stream_bytes += self.connection.read(1024)
                # print(stream_bytes)
                first = stream_bytes.find(b'\xff\xd8')
                last = stream_bytes.find(b'\xff\xd9')

                # controlCommand = '-1'  # do nothing

                if first != -1 and last != -1:
                    jpg = stream_bytes[first:last + 2]
                    stream_bytes = stream_bytes[last + 2:]

                    image = cv2.imdecode(np.frombuffer(jpg, dtype=np.uint8),
                                         cv2.IMREAD_COLOR)

                    gray = cv2.imdecode(np.frombuffer(jpg, dtype=np.uint8),
                                        cv2.IMREAD_GRAYSCALE)

                    cv2.imshow('image', image)
                    ''' Now process the image '''
                    if stop_flag is False:
                        ########### Get control code to follow line ############
                        signal = self.follow_line(gray, image.copy())

                        # Get station code, width and height to calculate signal
                        station_code, width, height, topx, topy = self.detect_sign(
                            image.copy())

                        if station_code > 0 and stop_sign_active is True:
                            print('\nstation_code: ' + str(station_code))

                            if request_num_customers is False:  # if not sent yet
                                # then request
                                # num_requests = self.getNumCustomerRequests(station_code)

                                num_requests = '1'
                                print('        + ' + num_requests +
                                      ' requests waiting at station ' +
                                      str(station_code))

                                if int(num_requests
                                       ) > 0:  # if there is request
                                    # Stop for 2 seconds
                                    if stop_flag is False:
                                        print('        => Stop')
                                        self.stop_start = cv2.getTickCount()
                                        stop_flag = True
                                        request_num_customers = True
                                        signal = 0  # send signal, stop
                        else:
                            self.stop_start = cv2.getTickCount()

                            if stop_sign_active is False:
                                self.drive_time_after_stop = (
                                    self.stop_start -
                                    self.stop_finish) / cv2.getTickFrequency()
                                if self.drive_time_after_stop > 5:
                                    stop_sign_active = True

                    else:
                        self.stop_finish = cv2.getTickCount()
                        self.stop_time = (self.stop_finish - self.stop_start
                                          ) / cv2.getTickFrequency()
                        print('        ... Stop time: %.2fs' % self.stop_time)

                        # 5 seconds later, continue driving
                        if self.stop_time > 2:
                            stop_flag = False
                            stop_sign_active = False
                            request_num_customers = False
                            print('        Waited for 2 seconds, go!')
                            signal = 1  # send signal, move forward

                    if signal != old_signal:
                        print('signal', signal)
                        old_signal = signal
                        # control(signal)

                    cv2.imwrite('data/leftt/' + str(frame_num) + '.jpg', image)

                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break

        finally:
            self.connection.close()
            self.listen_socket.close()
예제 #32
0
def robotcontrol(
    threadname
):  # controls the robot and estimates its pose based on odometry.
    """
    :Input --> w1: Current angular speed of the right motor.
    :Input --> w2: Current angular speed of the left motor.
    :Input --> ww1: Target angular speed for the right motor.
    :Input --> ww2: Target angular speed for the left motor.
    :Input --> vela: current PWM for the right motor.
    :Input --> velb: current PWM for the left motor.
    :Output --> vela: New PWM value for the right motor.
    :Output --> velb: New PWM value fot the left motor
    This function controls the motors so that their target angular speed is reached. As the voltage for the motors is
    unknown the correct PWM is obtained by it's error between the target speed linear and angular speeds and the
    encoders readings. This function also makes the odometry based estimations of the robot pose.
    """
    file = open('data1.txt', 'w')
    file.write('velocidadlineal    Velocidadangular    nact\
                xest    yest    thetaest    control    xact    yact   theatact\
                    covariance' + '\n')

    # Test one:
    global actualization, x_act, y_act, theta_act, control, x_est, y_est, theta_est,\
        vlin, vang, nact, error, strait, done, v_correction

    ltime = 0  # Last time the encoders were read

    # Initial speeds
    vang = 0
    vlin = 0
    lcounta = 0
    lcountb = 0

    # Other
    count = 0  # times the linear speed is lower than the minimum
    out = 0

    done = False

    lnact = 0

    # kalman
    pk = ppk

    while control:  # Start control
        """
        # Read encoders
        Only update the time only if the count isn´t to avoid missing very low speeds ((((not done)))), speeds can't be
        that low. Calculate the angular speed of the motors and the linear and angular speed of the robot. There is only
        one encoder, so we can only read the speed not the direction. to correct this we adjust the direction to match
        the control set to the motors.

        doing this one count can be lost nad counted in the next step, as there are many marks 24 per revolution
        this isn`t a big issue
        """
        # Angular speed of the motors

        time = cv2.getTickCount() / cv2.getTickFrequency()
        da = counta - lcounta
        db = countb - lcountb
        # print('da: '+ str(da))
        # print('db: '+ str(db))
        # print('Count a: ' + str(counta))
        # print('Count b: ' + str(countb))

        lcounta = counta
        lcountb = countb

        w1 = (da * np.pi) / (marks * (time - ltime))
        w2 = (db * np.pi) / (marks * (time - ltime))
        """
        # get position actualization
        If the robots corrects its pose estimation using the camera sensor it has to be updated. This step is important
        as the odometry based pose estimation error grows as the robot moves. The pose actualization is applied to the
        last known pose. take care not to make the actualization after the new speed if calculated---------------------------------
        """

        # linear and angular speeds of the robot (not the target ones)
        # lvlin = vlin
        # lvang = vang
        vlin = r * (w1 + w2) / 2
        vang = r * (w1 - w2) / l

        # Prediction phase:
        # pose prediction
        # Estimate the new robot pose

        y_est += (time - ltime) * vlin * np.sin(theta_est)
        x_est += (time - ltime) * vlin * np.cos(theta_est)

        y_act += (time - ltime) * vlin * np.sin(theta_act)
        x_act += (time - ltime) * vlin * np.cos(theta_act)

        theta_est += (time - ltime) * vang
        theta_act += (time - ltime) * vang

        if theta_est > np.pi:  # normalise theta_est
            theta_est -= 2 * np.pi
        elif theta_est < -np.pi:
            theta_est += 2 * np.pi
        if theta_act > np.pi:  # normalise theta_est
            theta_act -= 2 * np.pi
        elif theta_act < -np.pi:
            theta_act += 2 * np.pi

        pos = np.matrix([[x_est], [y_est], [theta_est]])
        # pose estimation covariance
        fk1 = np.matrix([[1, 0, -vlin * (time - ltime) * np.sin(theta_est)],
                         [0, 1, vlin * (time - ltime) * np.cos(theta_est)],
                         [0, 0, 1]])  # Jacobian
        pk1 = fk1 * pk * np.transpose(fk1) + q  # covariance matrix

        # Actualization phase
        if nact - lnact >= 10:  # only check every 10 iterations
            lnact = nact
            # check frame
            ret, frame = cap.read()
            ret, frame = cap.read()
            ret, frame = cap.read()
            ret, frame = cap.read()
            ret, frame = cap.read()
            if ret:
                # correct frame
                h, w = frame.shape[:2]
                newcameramtx, roi = cv2.getOptimalNewCameraMatrix(
                    matrix, distortion, (w, h), 1, (w, h))
                frame = cv2.undistort(frame, matrix, distortion, None,
                                      newcameramtx)
                x, y, w, h = roi
                frame = frame[y:y + h, x:x + w]
                ret, length, image_center, real_center, fframe = localization.pattern(
                    frame)
                if ret and length > 0:
                    print('LANDMARK detected¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡')
                    # estimate the image points given the global coordinates of the points
                    # and also its jacobian
                    tg_c = np.matrix([[
                        np.sin(theta_est), -np.cos(theta_est), 0, ym +
                        y_est * np.cos(theta_est) - x_est * np.sin(theta_est)
                    ], [0, 0, -1, zm],
                                      [
                                          np.cos(theta_est),
                                          np.sin(theta_est), 0,
                                          -xm - x_est * np.cos(theta_est) -
                                          y_est * np.sin(theta_est)
                                      ], [0, 0, 0, 1]])
                    # print(real_center)
                    dd = False
                    for i in real_center:
                        # get camera coordinates
                        c = tg_c * np.transpose(i)
                        # print(c)
                        # project to image
                        u = matrix.item(2) + matrix.item(0) * (c.item(0) /
                                                               c.item(2))
                        v = matrix.item(5) + matrix.item(4) * (c.item(1) /
                                                               c.item(2))
                        if dd:
                            est_im_centers = np.vstack(
                                [est_im_centers, [u], [v]])
                            hk = np.vstack([
                                hk,
                                vision_jacobian(x_est, y_est, theta_est, i)
                            ])
                        else:
                            est_im_centers = np.matrix([[u], [v]])
                            hk = vision_jacobian(x_est, y_est, theta_est, i)
                            dd = True

                    # innovation
                    print('img: ' + str(image_center))
                    yk = image_center - est_im_centers
                    # Innovation covariance
                    length, wide = np.shape(yk)
                    print('yk: ' + str((yk)))
                    # yk = yk.reshape((2*length,1))
                    rk = np.identity(length) * var_v
                    sk = hk * pk1 * np.transpose(hk) + rk
                    # Kalman gain
                    kk = pk1 * np.transpose(hk) * np.linalg.inv(sk)
                    print(kk)
                    # Correct the estimation
                    print('pos antes' + str(pos))
                    pos = pos + kk * (yk)
                    print('pos despues' + str(pos))
                    print('pos sin' + str([x_act, y_act, theta_act]))
                    x_est = pos.item(0)
                    y_est = pos.item(1)
                    theta_est = pos.item(2)
                    # Covariance actualization
                    pk = (np.identity(3) - kk * hk) * pk1
                else:
                    pk = pk1
            else:
                pk = pk1

        # error correction
        if strait:
            # voltage correction
            if vlin < 10:  # if the linear speed is lower than 10 cm/s the voltage is low
                print('LOW VOLTAGE' + str(count))
                if count >= 4:
                    v_correction += 3
                    count = 0
                else:
                    count += 1
            else:
                count = 0
            if vlin > 24:  # if the linear speed is too high the speed must be adjusted
                v_correction -= 3
            # robot control
            output = pose_control()
            change_speed(False, 2, output, 0, 0)

        file.write(
            str(vlin) + '    ' + str(vang) + '    ' + str(nact) + '    ' +
            str(x_est) + '    ' + str(y_est) + '    ' + str(theta_est) +
            '    ' + str(out) + '    ' + str(x_act) + '    ' + str(y_act) +
            '    ' + str(theta_act) + '    ' + str(pk) + '\n')
        ltime = time
        nact += 1
        sleep(0.2)

    done = True
    file.close()
예제 #33
0

while True:
    timer = cv2.getTickCount()
    success, img = cap.read()
    success, bbox = tracker.update(img)
    if success:
        drawBox(img, bbox)
    else:
        cv2.putText(img, "lost", (75, 75), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
                    (0, 0, 255))

    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)
    cv2.putText(img, str(int(fps)), (75, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
                (0, 0, 255))

    out.write(img)
    cv2.imshow("Tracking", img)

    if cv2.waitKey(1) & 0xff == ord('q'):
        break

cap.release()
out.release()
cv2.destroyAllWindows()
cap = cv.VideoCapture(args.input if args.input else 0)
while cv.waitKey(1) < 0:
    hasFrame, frame = cap.read()
    if not hasFrame:
        cv.waitKey()
        break

    frameHeight = frame.shape[0]
    frameWidth = frame.shape[1]

    # Create a 4D blob from a frame.
    inpWidth = args.width if args.width else frameWidth
    inpHeight = args.height if args.height else frameHeight
    blob = cv.dnn.blobFromImage(frame, args.scale, (inpWidth, inpHeight), args.mean, args.rgb, crop=False)

    # Run a model
    net.setInput(blob)
    if net.getLayer(0).outputNameToIndex('im_info') != -1:  # Faster-RCNN or R-FCN
        frame = cv.resize(frame, (inpWidth, inpHeight))
        net.setInput(np.array([[inpHeight, inpWidth, 1.6]], dtype=np.float32), 'im_info')
    outs = net.forward(getOutputsNames(net))

    postprocess(frame, outs)

    # Put efficiency information.
    t, _ = net.getPerfProfile()
    label = 'Inference time: %.2f ms' % (t * 1000.0 / cv.getTickFrequency())
    cv.putText(frame, label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))

    cv.imshow(winName, frame)
예제 #35
0
def main_function(testCaseNumber):

    #Defining constants
    basePath = "./Images/"
    print "Example Number : ", testCaseNumber
    tNo = "1"
    pNo = "2"
    testCaseNumber = str(testCaseNumber)
    trainingImagePath = basePath + testCaseNumber + "/" + tNo + ".jpg"
    grayscaleImagePath = basePath + testCaseNumber + "/" + pNo + "G.jpg"
    outputImagePath = basePath + testCaseNumber + "/output.jpg"
    k = 5
    try:
        os.stat("./../temp/" + testCaseNumber + "/")
    except:
        os.mkdir("./../temp/" + testCaseNumber + "/")
    #Reading Training Image
    """trainingImage = cv2.imread(trainingImagePath)
	trainingImage = cv2.cvtColor(trainingImage,cv2.COLOR_BGR2LAB)
	m,n,_ = trainingImage.shape
	print "Color Quantization : "
	#Preprocessing variable from image
	l = trainingImage[:,:,0]
	a = trainingImage[:,:,1]
	b = trainingImage[:,:,2]
	
	scaler = preprocessing.MinMaxScaler()
	pca = PCA(32)

	qab,centroid = quantization(a,b,k)
	print centroid
	# with open('./../temp/'+testCaseNumber+'/centroids', 'w') as csvfile:
	# 	writer = csv.writer(csvfile)
	# 	[writer.writerow(r) for r in centroid]

	t2 = cv2.getTickCount()
	t = (t2 - t1)/cv2.getTickFrequency()
	print "Time for quantization : ",t," seconds"
	
	print "Feature extraction : "
	feat,classes = getKeyPointFeatures(l,qab)
	print "Length of feature descriptor before PCA : ",len(feat[0])
	feat = scaler.fit_transform(feat)
	feat = pca.fit_transform(feat)
	print "Length of feature descriptor after PCA : ",len(feat[0])
	
	t3 = cv2.getTickCount()
	t = (t3 - t2)/cv2.getTickFrequency()
	print "Time for feature extraction : ",t," seconds"
	
	print "Training : "
	svm_classifier = train(feat,classes,k)
	t4 = cv2.getTickCount()
	t = (t4 - t3)/cv2.getTickFrequency()
	print "Time for training: ",t," seconds"
	"""

    centroid, svm_classifier, scaler, pca = modelSvm(trainingImagePath, k)
    t4 = cv2.getTickCount()
    print "Prediction : "

    grayscaleImage = cv2.imread(grayscaleImagePath, 0)
    outputImage, probabilityValues = predict(svm_classifier, grayscaleImage,
                                             centroid, scaler, pca)
    #Writing temporary objects to disk
    #Remove later
    #cv2.imwrite("./../temp/"+testCaseNumber+"/labTempOut.jpg",outputImage)
    #outputTempImageBGR = cv2.cvtColor(outputImage,cv2.COLOR_LAB2BGR)
    #cv2.imwrite("./../temp/"+testCaseNumber+"/BGRTempOut.jpg",outputTempImageBGR)
    #with open('./../temp/'+testCaseNumber+'/probVal', 'w') as csvfile:
    #	writer = csv.writer(csvfile)
    #	[writer.writerow(r) for r in probabilityValues]

    outputImage = postProcess(outputImage, centroid, probabilityValues)

    t5 = cv2.getTickCount()
    t = (t5 - t4) / cv2.getTickFrequency()
    print "Time for prediction : ", t, " seconds"
    #t = (t5 - t1)/cv2.getTickFrequency()
    #print "Total time : ",t," seconds"

    #trainingImage = cv2.cvtColor(trainingImage,cv2.COLOR_LAB2BGR)
    cv2.imwrite(outputImagePath, outputImage)
    #cv2.imshow("Training",trainingImage)
    cv2.imshow("Original", grayscaleImage)
    cv2.imshow("Predicted", outputImage)
    cv2.waitKey()
    cv2.destroyAllWindows()
예제 #36
0
def main():
    # Create Kinect object and initialize
    kin = kinz.Kinect(resolution=1080,
                      wfov=True,
                      binned=True,
                      framerate=30,
                      imu_sensors=False,
                      body_tracking=True)

    # Get depth aligned with color?
    align_frames = False
    image_scale = 0.5  # visualized image scale

    # initialize fps counter
    t = cv2.getTickCount()
    fps_count = 0
    fps = 0

    while True:
        if fps_count == 0:
            t = cv2.getTickCount()

        # read kinect frames. If frames available return 1
        if kin.get_frames(get_color=True,
                          get_depth=True,
                          get_ir=False,
                          get_sensors=False,
                          get_body=True,
                          get_body_index=True,
                          align_depth=align_frames):

            color_data = kin.get_color_data()
            depth_data = kin.get_depth_data()
            bodies = kin.get_bodies()
            body_index_data = kin.get_body_index_map(returnId=True,
                                                     inColor=False)

            print("bodies:", bodies)

            # extract frames to np arrays
            depth_image = np.array(depth_data.buffer, copy=True)
            color_image = np.array(color_data.buffer,
                                   copy=True)  # image is BGRA
            color_image = cv2.cvtColor(color_image,
                                       cv2.COLOR_BGRA2BGR)  # to BGR
            body_index_image = np.array(body_index_data.buffer, copy=True)
            print(body_index_image.shape)

            # Apply colormap on depth image (image must be converted to 8-bit per pixel first)
            depth_colormap = cv2.applyColorMap(
                cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)

            # Apply colormap on body index image
            body_index_image = cv2.applyColorMap(body_index_image * 10,
                                                 cmapy.cmap('tab20'))

            # Draw bodies on the RGB image
            draw_keypoints(color_image, bodies, img_type='rgb')

            # Draw bodies on the depth image
            draw_keypoints(depth_colormap, bodies, img_type='depth')

            # Resize images
            if align_frames:
                depth_colormap = cv2.resize(depth_colormap,
                                            None,
                                            fx=image_scale,
                                            fy=image_scale)
                body_index_image = cv2.resize(body_index_image,
                                              None,
                                              fx=image_scale,
                                              fy=image_scale)

            color_small = cv2.resize(color_image,
                                     None,
                                     fx=image_scale,
                                     fy=image_scale)
            size = color_small.shape[0:2]
            cv2.putText(color_small, "{0:.2f}-FPS".format(fps),
                        (20, size[0] - 20), cv2.FONT_HERSHEY_COMPLEX, 0.8,
                        (0, 0, 255), 2)

            cv2.imshow('Depth', depth_colormap)
            cv2.imshow('Color', color_small)
            cv2.imshow('Body index', body_index_image)

        k = cv2.waitKey(1) & 0xFF
        if k == 27:
            break
        elif k == ord('s'):
            cv2.imwrite("color.jpg", color_image)
            print("Image saved")

        # increment frame counter and calculate FPS
        fps_count = fps_count + 1
        if (fps_count == 30):
            t = (cv2.getTickCount() - t) / cv2.getTickFrequency()
            fps = 30.0 / t
            fps_count = 0

    kin.close()  # close Kinect
    cv2.destroyAllWindows()
예제 #37
0
def run_tracker(tracker, img, gt, video_name, restart=True):
    frame_counter = 0
    lost_number = 0
    toc = 0
    pred_bboxes = []
    if restart:  
        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:
                    
                    pred_bboxes.append(pred_bbox)
                else:
                    
                    pred_bboxes.append(2)
                    frame_counter = idx + 5 
                    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
예제 #38
0
    # compute the center of the contour
    M = cv2.moments(contours[biggest][:])
    cX = int(M["m10"] / M["m00"])
    cY = int(M["m01"] / M["m00"])
    button.ledarray(cX,cY)

    # draw the contour and center of the shape on the image
#    cv2.circle(image, (cX, cY), 7, (255, 0, 255), -1)
#    cv2.drawContours(image, contours, biggest, (0,255,0), 1)
    
    eroded = cv2.bitwise_and(image,image, mask= emask)
#    cv2.imshow('eroded',eroded)

    #end timer
    e2 = cv2.getTickCount()
    time = (e2 - e1)/ cv2.getTickFrequency()
    totaltime += time
    loops += 1
    count += 1
    if count > 2:
        fps = loops/time
        print(fps)
        count = 0
        loops = 0
        totaltime = 0
    #display frames per sec
    
##    cv2.putText(eroded,str(int(fps)),(50,50),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,255),2)

###record video
##    if ret==True:
                z = poses_3d_copy[:, 2::4]
                poses_3d[:, 0::4], poses_3d[:,
                                            1::4], poses_3d[:,
                                                            2::4] = -z, x, -y

                poses_3d = poses_3d.reshape(poses_3d.shape[0], 19, -1)[:, :,
                                                                       0:3]
                edges = (Plotter3d.SKELETON_EDGES +
                         19 * np.arange(poses_3d.shape[0]).reshape(
                             (-1, 1, 1))).reshape((-1, 2))
            plotter.plot(canvas_3d, poses_3d, edges)

            draw_poses(frame, poses_2d)

            current_time = (cv2.getTickCount() -
                            current_time) / cv2.getTickFrequency()
            if mean_time == 0:
                mean_time = current_time
            else:
                mean_time = mean_time * 0.95 + current_time * 0.05

            fps = int(1 / mean_time * 10) / 10

            cv2.putText(frame, 'processing FPS: {}'.format(fps), (40, 80),
                        cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255))
            #            cv2.imshow('ICV 3D Human Pose Estimation', frame)
            # cv2.imwrite("./data/frame-{}.png".format(i), frame)
            i += 1

            #            print("[INFO] done with frame {}".format(i))
예제 #40
0
while True:
    ret, image = cap.read()
    if ret is False:
        break
    #     image = cv.flip(image, 1)
    h, w = image.shape[:2]
    origin = image.copy()
    # 基于多个Region层输出getUnconnectedOutLayersNames
    blobImage = cv.dnn.blobFromImage(image, 1.0 / 255.0, (416, 416), None, True, False)
    outNames = net.getUnconnectedOutLayersNames()
    net.setInput(blobImage)
    outs = net.forward(outNames)

    # Put efficiency information.
    t, _ = net.getPerfProfile()
    fps = 1000 / (t * 1000.0 / cv.getTickFrequency())
    label = 'FPS: %.2f' % fps
    # cv.putText(image, label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)

    # 绘制检测矩形
    classIds = []
    confidences = []
    boxes = []
    for out in outs:
        for detection in out:
            scores = detection[5:]
            classId = np.argmax(scores)
            confidence = scores[classId]
            # numbers are [center_x, center_y, width, height]
            if confidence > 0.1:
                center_x = int(detection[0] * w)
예제 #41
0
    out = output_data[0]
    #out = np.argmax(output_data)

    return out


class_names = ["background", "pharmaceutical", "sharps", "trace_chemo"]

# Set up camera constants
#MAX is 1280
IM_WIDTH = 640
scale = 1 / 2

# Initialize frame rate calculation
frame_rate_calc = 1
freq = cv2.getTickFrequency()
font = cv2.FONT_HERSHEY_SIMPLEX

# Initialize Picamera and grab reference to the raw capture
camera = PiCamera()
camera.resolution = (IM_WIDTH, IM_WIDTH)
camera.framerate = 5
rawCapture = PiRGBArray(camera, size=(IM_WIDTH, IM_WIDTH))
rawCapture.truncate(0)

try:
    for frame1 in camera.capture_continuous(rawCapture,
                                            format="bgr",
                                            use_video_port=True):

        frame = frame1.array
예제 #42
0
rows, cols, channels = img2.shape
roi = img1[0:rows, 0:cols]
cv2.imshow("the position==0", roi)

# Now create a mask of logo and create its inverse mask also
img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
cv2.imshow('mask--1', img2gray)  #cheak the mask
ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)
cv2.imshow('inverse mask--2', mask_inv)

# Now black-out the area of logo in ROI
img1_bg = cv2.bitwise_and(roi, roi, mask=mask_inv)
cv2.imshow('img1_bg--3 the and on img1', img1_bg)

# Take only region of logo from logo image.
img2_fg = cv2.bitwise_and(img2, img2, mask=mask)
cv2.imshow('img2-fg--4 the and on img2fg', img2_fg)

# Put logo in ROI and modify the main image
dst = cv2.add(img1_bg, img2_fg)
img1[0:rows, 0:cols] = dst

cv2.imshow('res', img1)

t2 = cv2.getTickCount()  #for measuring performance
time = (t2 - t1
        ) / cv2.getTickFrequency()  #diffrence to get actual time of execution
print("the total time is==", time)
cv2.waitKey(0)
cv2.destroyAllWindows()
예제 #43
0
    def run(self) -> None:
        input_details = self.__interpreter.get_input_details()
        output_details = self.__interpreter.get_output_details()
        height = input_details[0]['shape'][1]
        width = input_details[0]['shape'][2]

        floating_model = (input_details[0]['dtype'] == np.float32)

        input_mean = 127.5
        input_std = 127.5

        # Initialize frame rate calculation
        frame_rate_calc = 1
        freq = cv2.getTickFrequency()

        # Initialize video stream
        videostream = VideoStream(resolution=(imW, imH), framerate=30).start()
        ct = CentroidTracker()
        # h = Head()
        time.sleep(1)
        frame_count = 0

        while (self.__alive):

            t1 = cv2.getTickCount()

            frame1 = videostream.read()
            frame_count += 1
            frame = frame1.copy()
            frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            frame_resized = cv2.resize(frame_rgb, (width, height))
            input_data = np.expand_dims(frame_resized, axis=0)

            if floating_model:
                input_data = (np.float32(input_data) - input_mean) / input_std

            self.__interpreter.set_tensor(input_details[0]['index'],
                                          input_data)
            self.__interpreter.invoke()

            boxes = self.__interpreter.get_tensor(output_details[0]['index'])[
                0]  # Bounding box coordinates of detected objects
            classes = self.__interpreter.get_tensor(
                output_details[1]['index'])[
                    0]  # Class index of detected objects
            scores = self.__interpreter.get_tensor(output_details[2]['index'])[
                0]  # Confidence of detected objects
            rects = []
            r = []
            val = []
            track_id = 0
            max_size = 0
            for i in range(len(scores)):
                if ((scores[i] > min_conf_threshold) and (scores[i] <= 1.0)):

                    object_name = self.__labels[int(classes[i])]
                    if object_name == "person":
                        rects = []
                        ymin = int(max(1, (boxes[i][0] * imH)))
                        rects.append(ymin)
                        xmin = int(max(1, (boxes[i][1] * imW)))
                        rects.append(xmin)
                        ymax = int(min(imH, (boxes[i][2] * imH)))
                        rects.append(ymax)
                        xmax = int(min(imW, (boxes[i][3] * imW)))
                        rects.append(xmax)
                        rects = [xmin, ymin, xmax, ymax]

                        val = np.array(rects)
                        r.append(val.astype("int"))
                        cv2.rectangle(frame, (xmin, ymin), (xmax, ymax),
                                      (10, 255, 0), 2)

                        #Draw label

                        object_name = self.__labels[int(
                            classes[i]
                        )]  # Look up object name from "labels" array using class index
                        xmid = xmin + ((xmax - xmin) / 2)
                        p = 640 - xmid

                        #angle = h.find_angle(p * (1/64))
                        #rot = h.rotate(angle)
                        #label = '%s: %d - %d%%' % (object_name, xmin,xmax) # Example: 'person: 72%'
                        #labelSize, baseLine = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.7, 2) # Get font size
                        #label_ymin = max(ymin, labelSize[1] + 10) # Make sure not to draw label too close to top of window
                        #cv2.rectangle(frame, (xmin, label_ymin-labelSize[1]-10), (xmin+labelSize[0], label_ymin+baseLine-10), (255, 255, 255), cv2.FILLED) # Draw white box to put label text in
                        #cv2.putText(frame, label, (xmin, label_ymin-7), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2) # Draw label text

    # Draw framerate in corner of frame

            objects = ct.update(r)
            #frame_size = output[1]

            #print(output)
            print(objects)
            cv2.putText(frame, 'FPS: {0:.2f}'.format(frame_rate_calc),
                        (30, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0),
                        2, cv2.LINE_AA)

            #All the results have been drawn on the frame, so it's time to display it.
            flag = 0
            next_id = 0
            i = 0
            new_coord = []
            next_coord = []
            coord = []
            for (objectID, centroid) in objects.items():
                if (objectID == track_id):
                    flag = 1
                    new_coord = centroid
                if (i == 0):
                    next_id = objectID
                    next_coord = centroid
                    i += 1
                text = "ID {}".format(objectID)
                cv2.putText(frame, text, (centroid[0] - 10, centroid[1] - 10),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
                cv2.circle(frame, (centroid[0], centroid[1]), 4, (0, 255, 0),
                           -1)

            if (flag == 0):
                track_id = next_id
                coord = next_coord
            else:
                coord = new_coord

            # head rotation

            cv2.imshow('Object detector', frame)

            # Calculate framerate
            t2 = cv2.getTickCount()
            time1 = (t2 - t1) / freq
            frame_rate_calc = 1 / time1

            # Press 'q' to quit
            if cv2.waitKey(1) == ord('q'):
                break
예제 #44
0
def clock():
    return cv.getTickCount() / cv.getTickFrequency()
import cv2

cap = cv2.VideoCapture(r"D:\Downloads\OpenCV\video.mp4")
count = 0
while True:

    timer1 = cv2.getTickCount()
    success, img = cap.read()

    timer2 = cv2.getTickCount()
    fps = cv2.getTickFrequency() / ((timer2 - timer1) * 10)
    cv2.putText(img, f'FPS: {fps}', (10, 50), cv2.FONT_HERSHEY_COMPLEX, 0.7,
                (0, 255, 0), 2)
    count = count + 1

    if success == True:
        cv2.imshow('Feed', img)
    else:
        cv2.destroyAllWindows()
        break

    if cv2.waitKey(100) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break

# print(count)
예제 #46
0
    with np.load(single_npz) as data:
        print(data.files)
        train_temp = data['train']
        train_labels_temp = data['train_labels']
        print(train_temp.shape)
        print(train_labels_temp.shape)
        image_array = np.vstack((image_array, train_temp))
        label_array = np.vstack((label_array, train_labels_temp))

train = image_array[1:, :]
train_labels = label_array[1:, :]
print(train.shape)
print(train_labels.shape)

e00 = cv2.getTickCount()
time0 = (e00 - e0) / cv2.getTickFrequency()
print('Loading image duration:', time0)

# set start time

e1 = cv2.getTickCount()

# create MLP

layer_sizes = np.int32([38400, 32, 4])
model = cv2.ml.ANN_MLP_create()

model.setLayerSizes(layer_sizes)
model.setTrainMethod(cv2.ml.ANN_MLP_BACKPROP)
model.setBackpropMomentumScale(0.0)
model.setBackpropWeightScale(0.001)
예제 #47
0
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt


img = cv.imread(cv.samples.findFile("opencv-logo-white.png"), cv.IMREAD_COLOR)


# 2D Convolution (Image Filtering)
kernel = np.ones((5, 5), np.float32) / 25
e1 = cv.getTickCount()
dst = cv.filter2D(img, -1, kernel)
e2 = cv.getTickCount()
t1 = (e2 - e1) / cv.getTickFrequency()


def custom_filter2D(img, kernel):
    ih, iw, ic = img.shape
    kh, kw = kernel.shape
    rows = ih + kh
    cols = iw + kw

    border_top = kh // 2
    border_bottom = rows - ih - border_top
    border_left = kw // 2
    border_right = cols - iw - border_left

    src = cv.copyMakeBorder(img, border_top, border_bottom, border_left, border_right, cv.BORDER_DEFAULT)

    assert src.shape[:2] == (rows, cols), "src.shape = {}, while (rows, cols) = {}".format(src.shape[:2], (rows, cols))
예제 #48
0
while True:
    ret, frame=cap.read()
    tab_image[i]=frame
    tickmark=cv2.getTickCount()
    mask=meth.calcul_mask(frame,image_back, seuil)
    elements=cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
    nbr=0
    for e in elements:
        ((x, y), rayon)=cv2.minEnclosingCircle(e)
        if rayon>20:
            cv2.circle(frame, (int(x)+xmin, int(y)+ymin), 5, color_infos, 10)
            nbr+=1
    if nbr>nbr_old:
        vehicule+=1
    nbr_old=nbr
    fps=cv2.getTickFrequency()/(cv2.getTickCount()-tickmark)
    cv2.putText(frame, "FPS: {:05.2f}  Seuil: {:d}".format(fps, seuil), (10, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, color_infos, 1)
    cv2.rectangle(frame, (xmin, ymin), (xmax+120, ymax), (255, 0, 0), 5)
    cv2.rectangle(frame, (xmax, ymin), (xmax+120, ymax), (255, 0, 0), cv2.FILLED)
    cv2.putText(frame, "{:04d}".format(vehicule), (xmax+10, ymin+35), cv2.FONT_HERSHEY_COMPLEX_SMALL, 2, (255, 255, 255), 2)
    cv2.imshow('video', frame)
    cv2.imshow('mask', mask)
    key=cv2.waitKey(1)&0xFF
    if key==ord('q'):
        break
    if key==ord('p'):
        seuil+=1
    if key==ord('m'):
        seuil-=1

cap.release()
예제 #49
0
def main(argv, prog=''):
    # Create an instance of the matting class
    mat = Matting()
    # Parse the command line arguments
    success, args, unprocessedArgv, msg = parseArguments(argv, mat, prog)

    if not success:
        print('Error: Argument parsing: ', msg)
        return success, unprocessedArgv

    # Initialize the variables for measuring timings
    t1 = t2 = t3 = t4 = 0
    if args.matting:
        # Get the value of the openCV timer
        t1 = cv.getTickCount()

        # Call the routine that reads the images and stores them in
        # the appropriate data structure stored with the matting
        # instance
        #
        # Note: The images themselves are supposed to be private
        # variables. They should be acccessed using the
        # readImage(), writeImage(), useTriangulationResults()
        # methods of the matting class. The images are referenced
        # by their string descriptor
        for (fname,
             key) in [(args.backA[0], 'backA'), (args.backB[0], 'backB'),
                      (args.compA[0], 'compA'), (args.compB[0], 'compB')]:
            success, msg = mat.readImage(fname, key)
            if not success:
                print('Error: %s' % msg)
                return success, unprocessedArgv

        # Get the value of the openCV timer
        t2 = cv.getTickCount()

        # Run the triangulation matting algorithm. The routine
        # returns a tuple whose first element is True iff the routine
        # was successfully executed and the second element is
        # an error message string (if the routine failed)
        print('Triangulation matting...')
        success, text = mat.triangulationMatting()
        if not success:
            print('Error: Triangulation matting routine failed: %s' % text)
            return success, unprocessedArgv

        # Get the value of the openCV timer
        t3 = cv.getTickCount()

        # Call the routine that writes to disk images stored in a matting
        # instance.
        # Note: The images themselves are supposed to be private variables
        #       They should be acccessed using the readImage(), writeImage()
        #       and useTriangulationResults() methods of the matting class
        #       The images are referenced by their string descriptor
        for (fname, key) in [(args.colOut[0], 'colOut'),
                             (args.alphaOut[0], 'alphaOut')]:
            success, msg = mat.writeImage(fname, key)
            if not success:
                print(msg)
                print('Error: Image %s cannot be written' % fname)
                return success, unprocessedArgv

        # Get the value of the openCV timer
        t4 = cv.getTickCount()

    elif args.compositing:
        t1 = cv.getTickCount()

        # Call the routine that reads the images and stores them in
        # the appropriate data structure stored with the matting
        # instance
        #
        # Note: The images themselves are supposed to be private
        # variables. They should be acccessed using the
        # readImage(), writeImage(), useTriangulationResults()
        # methods of the matting class. The images are referenced
        # by their string descriptor
        for (fname, key) in [(args.colIn[0], 'colIn'),
                             (args.alphaIn[0], 'alphaIn'),
                             (args.backIn[0], 'backIn')]:
            success, msg = mat.readImage(fname, key)
            if not success:
                print('Error: %s' % msg)
                return success, unprocessedArgv

        print('Compositing...')
        t2 = cv.getTickCount()
        # Run the compositing algorithm. The routine
        # returns a tuple whose first element is True iff the routine
        # was successfully executed and the second element is
        # an error message string (if the routine failed)
        success, text = mat.createComposite()
        if not success:
            print('Error: %s' % text)
            return success, unprocessedArgv

        t3 = cv.getTickCount()
        success, msg = mat.writeImage(args.compOut[0], 'compOut')
        if not success:
            print(msg)
            print('Error: Image %s cannot be written' % fname)
            return success, unprocessedArgv
        t4 = cv.getTickCount()

    print(
        '----------------------------------\nTimings\n----------------------------------'
    )
    print('Reading:    %g seconds' % ((t2 - t1) / cv.getTickFrequency()))
    print('Processing: %g seconds' % ((t3 - t2) / cv.getTickFrequency()))
    print('Writing:    %g seconds' % ((t4 - t3) / cv.getTickFrequency()))

    # return any command-line arguments that were not processed
    return success, unprocessedArgv
예제 #50
0
    return img_bgr


detector_hog = dlib.get_frontal_face_detector()
landmark_predictor = dlib.shape_predictor(
    './models/shape_predictor_68_face_landmarks.dat')

vc = cv2.VideoCapture('./images/video2.mp4')
img_sticker = cv2.imread('./images/king.png')

vlen = int(vc.get(cv2.CAP_PROP_FRAME_COUNT))
print(vlen)  # 비디오 프레임의 총 개수

for i in range(vlen):
    ret, img = vc.read()
    if ret == False:
        break

    ## 추가된 부분
    start = cv2.getTickCount()
    img_result = img2sticker_orig(img, img_sticker.copy(), detector_hog,
                                  landmark_predictor)
    time = (cv2.getTickCount() - start) / cv2.getTickFrequency() * 1000
    print('[INFO] time: %.2fms' % time)

    cv2.imshow('show', img_result)
    key = cv2.waitKey(1)
    if key == 27:
        break
예제 #51
0
    def spin(self):
        global lidarLaunch
        threshold_value = 50
        global starter, pub, greenLight, aP, lastP, kernel
        global aP_kf
        global last_lane_base, last_LorR
        global final_cmd, cam_cmd
        last_lane_base = -1
        last_LorR = 1
        y_nearest = 0
        while not rospy.is_shutdown():
            ret, img = self.cap.read()
            if ret == True:
                if (not lidarLaunch):
                    c1 = cv2.getTickCount()
                    kfObj = KalmanFilter()
                    predictedCoords = np.zeros((2, 1), np.float32)
                    #img = cv2.pyrDown(img)
                    undstrt = cv2.undistort(img, intrinsicMat, distortionCoe,
                                            None, intrinsicMat)
                    #self.puborignialI.publish(self.cvb.cv2_to_imgmsg(undstrt))

                    #gray0 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
                    gray = cv2.cvtColor(undstrt, cv2.COLOR_BGR2GRAY)

                    #gray_warped = perspectiveTrans(gray)
                    #self.puborignialI.publish(self.cvb.cv2_to_imgmsg(gray_warped))
                    #cv2.waitKey(1)

                    #gray[:gray.shape[0]/3,:] = 0

                    #frame_gray = cv2.GaussianBlur(frame_gray, (5,5),0)
                    #cv2.imshow('gray',gray)
                    #cv2.waitKey(1)

                    #origin_thr = np.zeros_like(gray_Blur)
                    #origin_thr[(gray_Blur <= 100)] = 255

                    #_, origin_thr = cv2.threshold(gray_Blur,threshold_value,255, cv2.THRESH_BINARY_INV)
                    global blind_detected
                    global blind_detection_flag
                    global delay_flag
                    if True:
                        print "...........Ramp_Control........."

                        if (blind_detected == False):
                            cam_cmd.linear.x = 0.1
                        if (blind_detection_flag == False) and (delay_flag
                                                                == False):
                            print "!!!!!!!!!!!!!!!!!!!!!!!!!delay_start............................................"
                            beginning_time = cv2.getTickCount()
                            delay_flag = True
                        if delay_flag == True:
                            current_time = cv2.getTickCount()
                            delay_time = (current_time - beginning_time
                                          ) / cv2.getTickFrequency()
                            print "delay_time:"
                            print delay_time
                            if delay_time >= 1:
                                delay_flag = False
                                blind_detection_flag = True
                                print "...................................delay end!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
                        if (blind_detected == False) and (blind_detection_flag
                                                          == True):
                            blind_detected, y_nearest = blind_detection(gray)
                        if (blind_detected == True) and (blind_detection_flag
                                                         == True):
                            print "__________ramp disappears_______________________"
                            cam_cmd.angular.z = 0
                            pub.publish(cam_cmd)
                            continue
                        _, binary_gray = cv2.threshold(
                            gray, 0, 255,
                            cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
                        binary_gray[:y_nearest, :] = 0
                        binary_warped = perspectiveTrans_slope(binary_gray)
                        margin = 30
                        histogram = np.sum(
                            binary_warped[binary_warped.shape[0] * 4 / 5:, :],
                            axis=0)  #//2
                    #print origin_thr.shape

                    #self.pubI.publish(self.cvb.cv2_to_imgmsg(origin_thr))
                    #_, origin_thr = cv2.threshold(origin_thr,threshold_value,255, cv2.THRESH_BINARY)
                    #self.pubI.publish(self.cvb.cv2_to_imgmsg(origin_thr))

                    #cv2.imshow('origin_thr',origin_thr)

                    lane_base = np.argmax(histogram)

                    nwindows = 10
                    window_height = int(binary_warped.shape[0] / nwindows)
                    nonzero = binary_warped.nonzero()
                    nonzeroy = np.array(nonzero[0])
                    nonzerox = np.array(nonzero[1])
                    lane_current = lane_base
                    #margin = 100
                    minpix = 1
                    lane_inds = []

                    binary_warped = cv2.cvtColor(binary_warped,
                                                 cv2.COLOR_GRAY2BGR)

                    for window in range(nwindows):
                        win_y_low = binary_warped.shape[0] - (
                            window + 1) * window_height
                        win_y_high = binary_warped.shape[
                            0] - window * window_height
                        win_x_low = lane_current - margin
                        win_x_high = lane_current + margin

                        cv2.rectangle(binary_warped, (win_x_low, win_y_low),
                                      (win_x_high, win_y_high), (255, 0, 0), 3)
                        good_inds = ((nonzeroy >= win_y_low) &
                                     (nonzeroy < win_y_high) &
                                     (nonzerox >= win_x_low) &
                                     (nonzerox < win_x_high)).nonzero()[0]

                        lane_inds.append(good_inds)
                        if len(good_inds) > minpix:
                            lane_current = int(np.mean(
                                nonzerox[good_inds]))  ####

                    lane_inds = np.concatenate(lane_inds)  #数组拼接

                    pixelX = nonzerox[lane_inds]
                    pixelY = nonzeroy[lane_inds]

                    # calculate the aimPoint

                    if (pixelX.size == 0):
                        continue
                    try:
                        a1, a0 = np.polyfit(pixelX, pixelY, 1)
                        ###### to draw the fitted curve

                        for i in range(binary_warped.shape[0] / 5):
                            y = 5 * i
                            x = int((y - a0) / a1)
                            cv2.circle(binary_warped, (x, y), 3, (0, 0, 255),
                                       -1)

                    except:
                        print "xxx"
                        cam_cmd.angular.z = 0
                        final_cmd = cam_cmd
                        pub.publish(final_cmd)
                        continue

                    aveX = np.average(pixelX)
                    cv2.line(binary_warped, (int(aveX), 0), (int(aveX), 479),
                             (0, 255, 255), 3)

                    roadWidth = 90 / (80 / 300.0)

                    if aveX < binary_warped.shape[1] / 2:
                        LorR = 1
                        #LeftLane
                    else:
                        LorR = -1
                        #RightLane
                    aimLaneP = [0, 0]
                    aimLaneP[1] = 450
                    aimLaneP[0] = aveX

                    aP[0] = aimLaneP[0] + LorR * roadWidth / 2
                    aP[1] = aimLaneP[1]

                    predictedCoords = kfObj.Estimate(aP[0], aP[1])
                    aP_kf[0] = predictedCoords[0][0]
                    aP_kf[1] = predictedCoords[1][0]

                    Y_half = np.argsort(pixelY)[int(len(pixelY) / 2)]
                    X_half = np.argsort(pixelX)[int(len(pixelY) / 2)]

                    aP_kf[0] = pixelX[X_half] + LorR * 200
                    aP_kf[1] = pixelY[Y_half] + LorR * 50

                    #aP[0] = aP_kf[0]
                    #aP[1] = aP_kf[1]

                    if aP[0] != aP[0] or aP[1] != aP[1]:
                        continue

                    cv2.circle(binary_warped, (int(aP[0]), int(aP[1])), 20,
                               (0, 0, 255), -1)
                    cv2.circle(binary_warped, (int(aP_kf[0]), int(aP_kf[1])),
                               20, (0, 255, 0), -1)

                    #计算目标点的真实坐标
                    x_cmPerPixel_slope = 80 / 300.0
                    y_cmPerPixel_slope = 80 / 1080.0
                    aP[0] = (aP[0] -
                             binary_warped.shape[1] / 2.0) * x_cmPerPixel_slope
                    aP[1] = (binary_warped.shape[0] -
                             aP[1]) * y_cmPerPixel_slope
                    '''
                    if(lastP[0] > 0.001 and lastP[1] > 0.001):
                        if(((aP[0]-lastP[0])**2 + (aP[1]-lastP[1])**2 > 1500) and Timer < 4 ): #To avoid the mislead by walkers
                            aP = lastP
                            Timer = Timer + 1
                        else:
                            Timer = 0
                    '''

                    lastP = aP
                    steerAngle = math.atan(2 * I * aP[0] /
                                           (aP[0] * aP[0] + (aP[1] + D) *
                                            (aP[1] + D)))
                    if steerAngle < 0:
                        cam_cmd.angular.z = k1 * steerAngle
                    else:
                        cam_cmd.angular.z = k2 * steerAngle
                    print 'steerAngle: %.5f' % (steerAngle)
                    #print(k*steerAngle*180/3.14)

                    #rospy.spinOnce()
                    final_cmd = cam_cmd
                    pub.publish(final_cmd)
                    self.pubI.publish(self.cvb.cv2_to_imgmsg(binary_warped))
                    c2 = cv2.getTickCount()
                    print 'time'
                    print(c2 - c1) / cv2.getTickFrequency()

                else:
                    pub.publish(final_cmd)

            self.rate.sleep()

        self.cap.release()
예제 #52
0
import cv2
from video_face_detector import VideoFaceDetector

camera = cv2.VideoCapture(0)

vfd = VideoFaceDetector('haarcascade_frontalface_default.xml', camera)

smooth_fps = 0
while True:
    start_time = cv2.getTickCount()

    frame = vfd.getFrameAndDetect()

    if vfd.isFaceFound:
        x, y, w, h = [int(i) for i in vfd.face()]
        p1 = (x, y)
        p2 = (x + w, y + h)
        cv2.rectangle(frame, p1, p2, (255, 0, 0))

    end_time = cv2.getTickCount()

    fps = cv2.getTickFrequency() / (end_time - start_time)
    smooth_fps = 0.9 * smooth_fps + 0.1 * fps

    print("FPS:", smooth_fps)

    cv2.imshow('Camera', frame)

    if cv2.waitKey(30) & 0xff == ord('q'):
        exit(0)
예제 #53
0
def main():
    tracker_types = ['BOOSTING', 'MIL', 'KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT'] 
    tracker_type = tracker_types[2]
     
    if int(minor_ver) < 3:
        tracker = cv2.Tracker_create(tracker_type)
    else:
        if tracker_type == 'BOOSTING':
            tracker = cv2.TrackerBoosting_create()
        if tracker_type == 'MIL':
            tracker = cv2.TrackerMIL_create()
        if tracker_type == 'KCF':
            tracker = cv2.TrackerKCF_create()
        if tracker_type == 'TLD':
            tracker = cv2.TrackerTLD_create()
        if tracker_type == 'MEDIANFLOW':
            tracker = cv2.TrackerMedianFlow_create()
        if tracker_type == 'GOTURN':
            tracker = cv2.TrackerGOTURN_create()
        if tracker_type == 'MOSSE':
            tracker = cv2.TrackerMOSSE_create()
        if tracker_type == "CSRT":
            tracker = cv2.TrackerCSRT_create()
    
    # 追跡する枠の座標とサイズ
    x = 200
    y = 200
    w = 224
    h = 224
    track_window=(x,y,w,h)
    # Reference Distance
    L0 = 100
    #S0 = 50176 #224x224

    # Base Distance
    LB = 100        
    # Define an initial bounding box
    bbox = (x, y, w, h)   #(287, 23, 86, 320) 
    
    drone = tellopy.Tello()
    drone.connect()

    container = av.open(drone.get_video_stream()) 
    drone.takeoff()
    #drone.is_autopilot="True"
    drone.is_autopilot="False"    
    
    while True:
        for frame in container.decode(video=0):
        
            image = cv2.cvtColor(numpy.array(frame.to_image()), cv2.COLOR_RGB2BGR)
               
            # Start timer
            timer = cv2.getTickCount()
 
            # Update tracker
            ok, bbox = tracker.update(image)
            # Calculate Frames per second (FPS)
            fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer);

            # Draw bounding box
            if ok:
                (x,y,w,h) = (int(bbox[0]),int(bbox[1]),int(bbox[2]),int(bbox[3]))
                CX=int(bbox[0]+0.5*bbox[2])
                CY=int(bbox[1]+0.5*bbox[3])
                S0=bbox[2]*bbox[3]
                print("CX,CY,S0=",CX,CY,S0)
                # Tracking success
                p1 = (x, y)
                p2 = (x + w, y + h)
                cv2.rectangle(image, p1, p2, (255,0,0), 2, 1)
            else :
                # Tracking failure
                cv2.putText(image, "Tracking failure detected", (100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2)
            # Display tracker type on frame
            cv2.putText(image, tracker_type + " Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2)
     
            # Display FPS on frame
            cv2.putText(image, "FPS : " + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2)
            cv2.imshow('test',image)

            key = cv2.waitKey(1)&0xff
            if key == ord('a'):
                drone.is_autopilot="True"
            elif key == ord('s'):
                drone.is_autopilot="False"            
            
            if drone.is_autopilot=="True":
                d = round(L0 * m.sqrt(S0 / (w * h)))
                dx = x + w/2 - CX
                dy = y + h/2 - CY
                print(d,dx,dy,drone.is_autopilot,w,h)
                tracking(drone,d,dx,dy,LB)
            elif drone.is_autopilot=="False":
                key_Operation(drone,key)
                print("key=",key,ord('q'))
                
            #key = cv2.waitKey(1)&0xff
            print("key=",key,ord('q'))
            if key == ord('q'):
                cv2.destroyAllWindows()
                break
            elif key == ord('r'):
                bbox = cv2.selectROI(image, False)
                print(bbox)
                (x,y,w,h) = (int(bbox[0]),int(bbox[1]),int(bbox[2]),int(bbox[3]))
                # Initialize tracker with first frame and bounding box
                ok = tracker.init(image, bbox)
                
        break
    drone.down(50)
    sleep(5)
    drone.land()    
    drone.subscribe(drone.EVENT_FLIGHT_DATA, handler)    
    drone.quit()   
예제 #54
0
        if c == 27:
            break


# src = cv.imread('musictreecut.jpg')
# cv.namedWindow('music', cv.WINDOW_AUTOSIZE)
# cv.imshow('music', src)
t1 = cv.getTickCount()

extrace_object()  # 抽取图像\视频中的指定对象(颜色)

# RGB图像的分离
# b, g, r = cv.split(src)
# cv.imshow('blue', b)
# cv.imshow('green', g)
# cv.imshow('red', r)
#
# # R、G、B合成
# src = cv.merge([b, g, r])
# src[:, :, 0] = 0
# cv.imshow('merged image', src)
# cv.imwrite('./yellowtree.png', src)

t2 = cv.getTickCount()
time = (t2 - t1) / cv.getTickFrequency()

print('Time: %f ms' % time)
cv.waitKey(0)

cv.destroyAllWindows()
예제 #55
0
        print("could not read from " + str(video_src) + " !\n")
        sys.exit(1)
    h, w = frame.shape[:2]
    prev_frame = frame.copy()
    motion_history = np.zeros((h, w), np.float32)
    hsv = np.zeros((h, w, 3), np.uint8)
    hsv[:, :, 1] = 255
    while True:
        ret, frame = cam.read()
        if ret == False:
            break
        frame_diff = cv2.absdiff(frame, prev_frame)
        gray_diff = cv2.cvtColor(frame_diff, cv2.COLOR_BGR2GRAY)
        thrs = cv2.getTrackbarPos('threshold', 'motempl')
        ret, motion_mask = cv2.threshold(gray_diff, thrs, 1, cv2.THRESH_BINARY)
        timestamp = cv2.getTickCount() / cv2.getTickFrequency()
        cv2.motempl.updateMotionHistory(motion_mask, motion_history, timestamp,
                                        MHI_DURATION)
        mg_mask, mg_orient = cv2.motempl.calcMotionGradient(motion_history,
                                                            MAX_TIME_DELTA,
                                                            MIN_TIME_DELTA,
                                                            apertureSize=5)
        seg_mask, seg_bounds = cv2.motempl.segmentMotion(
            motion_history, timestamp, MAX_TIME_DELTA)

        visual_name = visuals[cv2.getTrackbarPos('visual', 'motempl')]
        if visual_name == 'input':
            vis = frame.copy()
        elif visual_name == 'frame_diff':
            vis = frame_diff.copy()
        elif visual_name == 'motion_hist':
예제 #56
0
def run_tracker(frame, truth_bbox, seq_val=True):
    # KCF tracker use (hog, fixed_Window, multi_scale, cnn)
    tracker = KCFTracker(False, True, False, True)
    count = 1
    if seq_val == False:
        cam = cv2.VideoCapture(0)
        tracker.init(truth_bbox, frame)
    elif seq_val == True:
        tracker.init(truth_bbox[0], frame)
        frame_num = len(truth_bbox)

    while True:
        if seq_val == False:
            ok, frame = cam.read()
            if ok == False: break
        elif seq_val == True:
            count += 1
            if count > frame_num: break
            # read img from seq_file
            if count < 10:
                img_path = test_seq + 'img/000' + str(count) + '.jpg'
            elif count < 100:
                img_path = test_seq + 'img/00' + str(count) + '.jpg'
            else:
                img_path = test_seq + 'img/0' + str(count) + '.jpg'
            frame = cv2.imread(img_path)

        timer = cv2.getTickCount()
        bbox = tracker.update(frame)
        bbox = list(map(int, bbox))
        fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer)

        # Tracking success
        p1 = (int(bbox[0]), int(bbox[1]))  # top,left
        p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3])
              )  # top+x,left+y = bottom,right
        # draw tracking result
        cv2.rectangle(frame, p1, p2, (255, 0, 0), 2, 1)
        if seq_val == True:
            t1 = (int(truth_bbox[count - 1][0]), int(truth_bbox[count - 1][1]))
            t2 = (int(truth_bbox[count - 1][0] + truth_bbox[count - 1][2]),
                  int(truth_bbox[count - 1][1] + truth_bbox[count - 1][3]))
            # draw ground_truth bbox
            cv2.rectangle(frame, t1, t2, (0, 255, 0), 2, 1)
            # get center of ground_truth bbox, get displacement!!
            tcx = truth_bbox[count - 1][0] + truth_bbox[count - 1][2] / 2.0
            tcy = truth_bbox[count - 1][1] + truth_bbox[count - 1][3] / 2.0
            tcx_pre = truth_bbox[count - 2][0] + truth_bbox[count - 2][2] / 2.0
            tcy_pre = truth_bbox[count - 2][1] + truth_bbox[count - 2][3] / 2.0
            print('ground_truth:', tcx, tcy, 'prev:', tcx_pre, tcy_pre,
                  ' ; displacement:', tcx - tcx_pre, tcy - tcy_pre)

            # using re_init when tracking failed, IoU<=0.5
            #box1,box2 = [p1[0],p1[1],p2[0],p2[1]], [t1[0],t1[1],t2[0],t2[1]]
            #if IoU(box1,box2) <= 0.5:
            #	tracker.init(truth_bbox[count-1], frame)
            #	print('###########\nTrack Fail in frame',count,'\nRe_init KCF\n###########!')

        # Put FPS
        cv2.putText(frame, "FPS : " + str(int(fps)), (100, 50),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2)

        cv2.imshow("Tracking", frame)

        # Exit if ESC pressed
        k = cv2.waitKey(1) & 0xff
        if k == 27:
            break

    if seq_val == False:
        cam.release()
    cv2.destroyAllWindows()
예제 #57
0
    def collect_image(self):

        saved_frame = 0
        total_frame = 0

        # collect images for training
        # print 'Start collecting images...'
        e1 = cv2.getTickCount()
        image_array = np.zeros((1, 38400))
        label_array = np.zeros((1, 4), 'float')

        # stream video frames one by one
        try:
            stream_bytes = b" "
            frame = 1
            while self.send_inst:
                data = self.connection.read(1024)
                stream_bytes += data
                first = stream_bytes.find(b'\xff\xd8')
                last = stream_bytes.find(b'\xff\xd9')
                if first != -1 and last != -1:
                    jpg = stream_bytes[first:last + 2]
                    stream_bytes = stream_bytes[last + 2:]
                    image = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),
                                         cv2.IMREAD_GRAYSCALE)

                    # select lower half of the image
                    roi = image[120:240, :]

                    # save streamed images
                    cv2.imwrite(
                        'training_images/frame{:>05}.jpg'.format(frame), image)

                    #cv2.imshow('roi_image', roi)
                    cv2.imshow('image', image)

                    # reshape the roi image into one row array

                    frame += 1
                    total_frame += 1

                    # get input from human driver
                    for event in pygame.event.get():
                        if event.type == KEYDOWN:
                            key_input = pygame.key.get_pressed()

                            # complex orders
                            if key_input[pygame.K_UP] and key_input[
                                    pygame.K_RIGHT]:
                                print("Forward Right")
                                image_array = np.vstack(
                                    (image_array, temp_array))
                                label_array = np.vstack(
                                    (label_array, self.k[1]))
                                saved_frame += 1

                            elif key_input[pygame.K_UP] and key_input[
                                    pygame.K_LEFT]:
                                print("Forward Left")
                                image_array = np.vstack(
                                    (image_array, temp_array))
                                label_array = np.vstack(
                                    (label_array, self.k[0]))
                                saved_frame += 1

                            elif key_input[pygame.K_DOWN] and key_input[
                                    pygame.K_RIGHT]:
                                print("Reverse Right")

                            elif key_input[pygame.K_DOWN] and key_input[
                                    pygame.K_LEFT]:
                                print("Reverse Left")

                            # simple orders
                            elif key_input[pygame.K_UP]:
                                print("Forward")
                                saved_frame += 1
                                image_array = np.vstack(
                                    (image_array, temp_array))
                                label_array = np.vstack(
                                    (label_array, self.k[2]))

                            elif key_input[pygame.K_DOWN]:
                                print("Reverse")
                                saved_frame += 1
                                image_array = np.vstack(
                                    (image_array, temp_array))
                                label_array = np.vstack(
                                    (label_array, self.k[3]))

                            elif key_input[pygame.K_RIGHT]:
                                print("Right")
                                image_array = np.vstack(
                                    (image_array, temp_array))
                                label_array = np.vstack(
                                    (label_array, self.k[1]))
                                saved_frame += 1

                            elif key_input[pygame.K_LEFT]:
                                print("Left")
                                image_array = np.vstack(
                                    (image_array, temp_array))
                                label_array = np.vstack(
                                    (label_array, self.k[0]))
                                saved_frame += 1

                            elif key_input[pygame.K_x] or key_input[
                                    pygame.K_q]:
                                print('exit')
                                self.send_inst = False
                                break

                        elif event.type == pygame.KEYUP:
                            1 + 1

            # save training images and labels
            train = image_array[1:, :]
            train_labels = label_array[1:, :]

            # save training data as a numpy file
            file_name = str(int(time.time()))
            directory = "training_data"
            if not os.path.exists(directory):
                os.makedirs(directory)
            try:
                np.savez(directory + '/' + file_name + '.npz',
                         train=train,
                         train_labels=train_labels)
            except IOError as e:
                print(e)

            e2 = cv2.getTickCount()
            # calculate streaming duration
            time0 = (e2 - e1) / cv2.getTickFrequency()
            print('Streaming duration:', time0)

            print(train.shape)
            print(train_labels.shape)
            print('Total frame:', total_frame)
            print('Saved frame:', saved_frame)
            print('Dropped frame', total_frame - saved_frame)

        finally:
            self.connection.close()
            self.server_socket.close()
예제 #58
0
def processVideo(file_path):
    global totalFrames
    cap = cv.VideoCapture(file_path)
    totalFrames = int(cap.get(cv.CAP_PROP_FRAME_COUNT))
    frame_points = []
    frames = []
    frame_num = 0

    while cv.waitKey(1) < 0:
        hasFrame, frame = cap.read()
        if not hasFrame:
            cv.waitKey()
            break

        frameWidth = frame.shape[1]
        frameHeight = frame.shape[0]
        inp = cv.dnn.blobFromImage(frame,
                                   inScale, (inWidth, inHeight), (0, 0, 0),
                                   swapRB=False,
                                   crop=False)
        net.setInput(inp)
        out = net.forward()

        assert (len(BODY_PARTS) <= out.shape[1])

        points = []
        for i in range(len(BODY_PARTS)):
            # Slice heatmap of corresponding body's part.
            heatMap = out[0, i, :, :]

            # Originally, we try to find all the local maximums. To simplify a sample
            # we just find a global one. However only a single pose at the same time
            # could be detected this way.

            # Only works for one dancer on screen
            _, conf, _, point = cv.minMaxLoc(heatMap)
            x = (frameWidth * point[0]) / out.shape[3]
            y = (frameHeight * point[1]) / out.shape[2]

            # Add a point if it's confidence is higher than threshold.
            # if args.thr:
            #   points.append((int(x), int(y)))
            points.append((int(x), int(y)) if conf > args.thr else None)

        # draw the body pose on the frame
        for pair in POSE_PAIRS:
            partFrom = pair[0]
            partTo = pair[1]
            assert (partFrom in BODY_PARTS)
            assert (partTo in BODY_PARTS)

            # gives an index into the points array
            idFrom = BODY_PARTS[partFrom]
            idTo = BODY_PARTS[partTo]

            if points[idFrom] and points[idTo]:
                cv.line(frame, points[idFrom], points[idTo], (0, 255, 0), 3)
                cv.ellipse(frame, points[idFrom], (3, 3), 0, 0, 360,
                           (0, 0, 255), cv.FILLED)
                cv.ellipse(frame, points[idTo], (3, 3), 0, 0, 360, (0, 0, 255),
                           cv.FILLED)

        frame_points.append(points)
        t, _ = net.getPerfProfile()
        freq = cv.getTickFrequency() / 1000
        # cv.putText(frame, '%.2fms' % (t / freq), (10, 20), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))
        print("Processed frame #" + str(frame_num))
        frame_num += 1
        frames.append(frame)
    return frames, frame_points
예제 #59
0
kf = kalman_init(stateSize, measSize, contrSize)

# loop init
############
video_capture = cv2.VideoCapture(0)
ret = True;
ticks = 0
found = False; 

while True:
    ret, frame = video_capture.read()    
    res = frame.copy()
    
    precTick = ticks
    ticks = cv2.getTickCount()
    dT = (ticks - precTick)/cv2.getTickFrequency()

##########################################################################
    if(found):
        kf.transitionMatrix[2/stateSize,2%stateSize] = dT;
        kf.transitionMatrix[9/stateSize, 9%stateSize] = dT;    
        state = kf.predict()
        #print(state)
        print_Box(res, state.transpose(), (255,0,0))       
    
    ballsBox = detect_faces(frame,res,face_cascade)#, eye_cascade)
    if (found):
        meas_state, new = meas_state_fill(ballsBox, state)       
        state_meas_fill(state_meas, meas_state,state)
        meas = meas_format(ballsBox, state_meas, meas_state,kf)
        kf.correct(meas);  
예제 #60
0
def prediction(file_name, load_time, engine, labels, face_engine, class_arr,
               emb_arr):

    file_name = file_name
    load_time = load_time
    engine = engine
    labels = labels
    face_engine = face_engine
    class_arr = class_arr
    emb_arr = emb_arr

    with tf.Graph().as_default():
        with tf.compat.v1.Session() as sess:

            frame = cv2.imread(file_name)
            print('file_name:', file_name)
            img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
            draw = ImageDraw.Draw(img)

            # cap = cv2.VideoCapture(0)

            t1 = cv2.getTickCount()
            ans = engine.detect_with_image(img,
                                           threshold=0.05,
                                           keep_aspect_ratio=False,
                                           relative_coord=False,
                                           top_k=10)
            img = numpy.asarray(img)

            print('img:', img)

            if ans:

                crop_img = crop_image(ans, frame)
                embs = Tpu_FaceRecognize(face_engine, crop_img)
                face_num = len(ans)
                face_class = ['Others'] * face_num
                for i in range(face_num):
                    diff = np.mean(np.square(embs[i] - emb_arr), axis=1)
                    min_diff = min(diff)
                    if min_diff < THRED:
                        index = np.argmin(diff)

                        face_class[i] = class_arr[index]

                print('Face_class:', face_class)
                print('Classes:', class_arr)
                """

                # If the input picture is not categorized, let user input the name
                if 'Others' in face_class:
                    # print("usagi")
                    for k in range(0, len(crop_img)):
                        new_class_name = input('Please input your name of class:')
                        new_save = cv2.cvtColor(crop_img[k], cv2.COLOR_BGR2RGB)
                        cv2.imwrite('pictures/' + str(new_class_name) + '.jpg', new_save)

                    Create_embeddings(face_engine)
                    class_arr, emb_arr = read_embedding('embedding_book/embeddings.h5')  
                    
                    """

                for count, obj in enumerate(ans):

                    print('-----------------------------------------')
                    if labels:
                        print(labels[obj.label_id])

                    print('Score = ', obj.score)
                    box = obj.bounding_box.flatten().tolist()
                    # Draw a rectangle and label
                    cv2.rectangle(img, (int(box[0]), int(box[1])),
                                  (int(box[2]), int(box[3])), (255, 255, 0), 2)
                    cv2.putText(img, '{}'.format(face_class[count]),
                                (int(box[0]), int(box[1]) - 5),
                                cv2.FONT_HERSHEY_PLAIN, 1, (255, 0, 0), 1,
                                cv2.LINE_AA)

            t2 = cv2.getTickCount()
            t = (t2 - t1) * 1000 / cv2.getTickFrequency()
            cv2.putText(img, 'inf_time: {:.2f}'.format(t), (5, 20),
                        cv2.FONT_HERSHEY_PLAIN, 1, (255, 0, 0), 1, cv2.LINE_AA)

            cv2.putText(img, 'A: Add new class', (5, 450),
                        cv2.FONT_HERSHEY_PLAIN, 1, (255, 0, 0), 1, cv2.LINE_AA)
            cv2.putText(img, 'Q: Quit', (5, 470), cv2.FONT_HERSHEY_PLAIN, 1,
                        (255, 0, 0), 1, cv2.LINE_AA)
            img_ = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
            # cv2.imshow('frame', img_)
            cv2.imwrite(file_name, img_)