Example #1
0
    def track(self, frame):
        # Resize for efficiency
        frame = cv2.pyrDown(frame)
        frame = cv2.cvtColor(frame, cv.CV_BGR2Lab)
        self.frame = frame

        l,a,b = self.hist
        # Convert to float32 so we can represent probability values
        frame = frame.astype(np.float32)
        lback = cv2.calcBackProject([frame], [0], l, [0,255], 1)
        aback = cv2.calcBackProject([frame], [1], a, [0,255], 1)
        bback = cv2.calcBackProject([frame], [2], b, [0,255], 1)

        # Combine channels and convert to image
        self.back = (aback * bback * lback ) * 255
        self.back = self.back.astype(np.uint8)

        tmp = np.array(self.back)
        loc, contours = util.contour(tmp)

        self.contours = contours

        # Scale loc back up due to pyrDown
        if loc != None:
            loc = [x*2 for x in loc if loc != None]

        return loc
Example #2
0
 def removeBackground(self, image):
     discValue = 10
     threshold = 1
     hsvt = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
     
     for roiHist in self.negHistograms:
         dst = cv2.calcBackProject([hsvt],[0,1],roiHist,[0,180,0,256],1)
         cv2.imshow('dst', dst)
         disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(discValue,discValue))
         cv2.filter2D(dst, -1,disc,dst)
         ret,thresh = cv2.threshold(dst,threshold,255,cv2.THRESH_BINARY_INV)
         thresh = cv2.merge((thresh,thresh,thresh))
         image = cv2.bitwise_and(image,thresh)
     
     
     for roiHist in self.posHistograms:
         dst = cv2.calcBackProject([hsvt],[0,1],roiHist,[0,180,0,256],1)
         #cv2.imshow('dst', dst)
         disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(discValue,discValue))
         cv2.filter2D(dst, -1,disc,dst)
         ret,thresh = cv2.threshold(dst,threshold,255,cv2.THRESH_BINARY)
         thresh = cv2.merge((thresh,thresh,thresh))
         image = cv2.bitwise_and(image,thresh)
         
         
         #res = np.hstack((thresh,res))
     
     cv2.imshow('backProj', image)
     return image
Example #3
0
def meanShift(hsv, f1, f2, rois, mask):
	if f1 is not None:
		term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
		#import pdb; pdb.set_trace()
		ff1 = cv2.normalize(f1,alpha=0,beta=255,norm_type=cv2.NORM_MINMAX)
		ff2 = cv2.normalize(f2,alpha=0,beta=255,norm_type=cv2.NORM_MINMAX)
		dst1 = cv2.calcBackProject([hsv], [0,2], ff1, [70, 180, 10, 255], 1)
		dst2 = cv2.calcBackProject([hsv], [0,2], ff2, [70, 180, 10, 255], 1)
		bMask = cv2.inRange(hsv, np.array((70., 0., 10.)), np.array((255.,255.,50.)))
		wMask = cv2.inRange(hsv, np.array((70., 0., 200.)), np.array((255.,255.,255.)))
		dst1 = cv2.bitwise_and(dst1, mask)
		dst2 = cv2.bitwise_and(dst2, mask)
		dst1 = cv2.bitwise_and(dst1, wMask)
		dst2 = cv2.bitwise_and(dst2, bMask)
		dstVis1 = cv2.applyColorMap(dst1, cv2.COLORMAP_JET)
		dstVis2 = cv2.applyColorMap(dst2, cv2.COLORMAP_JET)

		nRois = []
		for (x0,y0), (x1,y1), flag in rois:
			if flag:
				x,y,w,h = cv2.meanShift(dst1, (x0,y0,x1-x0,y1-y0), term_crit)[1]
				nRois.append( [(x, y), (x+w, y+h), flag] )
				dst1[y:y+h, x:x+w] = 0
			else:
				x,y,w,h = cv2.meanShift(dst2, (x0,y0,x1-x0,y1-y0), term_crit)[1]
				nRois.append( [(x, y), (x+w, y+h), flag] )
				dst2[y:y+h, x:x+w] = 0
		
		#cv2.imshow('dst1', dstVis1)
		#cv2.imshow('dst2', dstVis2)
		return nRois
	return []
Example #4
0
  def update(self, frame):
    # print "updating %d " % self.id
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    back_project = cv2.calcBackProject([hsv],[0], self.roi_hist,[0,180],1)
    
    if args.get("algorithm") == "c":
      ret, self.track_window = cv2.CamShift(back_project, self.track_window, self.term_crit)
      pts = cv2.boxPoints(ret)
      pts = np.int0(pts)
      self.center = center(pts)
      cv2.polylines(frame,[pts],True, 255,1)
      
    if not args.get("algorithm") or args.get("algorithm") == "m":
      ret, self.track_window = cv2.meanShift(back_project, self.track_window, self.term_crit)
      x,y,w,h = self.track_window
      self.center = center([[x,y],[x+w, y],[x,y+h],[x+w, y+h]])  
      cv2.rectangle(frame, (x,y), (x+w, y+h), (255, 255, 0), 2)

    self.kalman.correct(self.center)
    prediction = self.kalman.predict()
    cv2.circle(frame, (int(prediction[0]), int(prediction[1])), 4, (255, 0, 0), -1)
    # fake shadow
    cv2.putText(frame, "ID: %d -> %s" % (self.id, self.center), (11, (self.id + 1) * 25 + 1),
        font, 0.6,
        (0, 0, 0),
        1,
        cv2.LINE_AA)
    # actual info
    cv2.putText(frame, "ID: %d -> %s" % (self.id, self.center), (10, (self.id + 1) * 25),
        font, 0.6,
        (0, 255, 0),
        1,
        cv2.LINE_AA)
Example #5
0
    def trackBiggestObject(self, frame):
        
        if self.biggestObject == None:
            return
        c = self.biggestObject[0]
        r = self.biggestObject[1]
        w = self.biggestObject[2]
        h = self.biggestObject[3]
        
        if w == 0 or h == 0:
            return
        
        #mask = cv2.inRange(hsv_roi, np.array((0., 0.,0.)), np.array((180.,255.,255.)))
        roi_hist = self.biggestObjectHistogram
        
        
    
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        #dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,256],1)
        dst = cv2.calcBackProject([hsv],[0,1],roi_hist,[0,180,0,256],1)
        #dst = cv2.calcBackProject([hsv],[0,1,2],roi_hist,[0,180,0,256,0,256],1)

        #ret, self.movingObjects[i] = cv2.meanShift(dst, track_window, self.term_crit)
        # apply meanshift to get the new location
        ret, self.biggestObject = cv2.CamShift(dst, self.biggestObject, self.term_crit)
        
        cv2.imshow('backproj',dst)
        # Draw it on image
        #pts = cv2.boxPoints(ret)
        #pts = np.int0(pts)
        #self.movingObjects.append(pts)
        #img2 = cv2.polylines(frame,[pts],True, 255,2)
        
        self.drawBiggestObjectContour(frame)
Example #6
0
 def camshift(self):
     self.imgs=[]
     while True:
         try:
             hsv = cv2.cvtColor(self.img, cv.CV_BGR2HSV)
             mask = cv2.inRange(hsv, np.array((0., 60., 32.)), np.array((180., 255., 255.)))
             x0, y0, w, h = self.bb
             x1 = x0 + w -1
             y1 = y0 + h -1
             hsv_roi = hsv[y0:y1, x0:x1]
             mask_roi = mask[y0:y1, x0:x1]
             hist = cv2.calcHist( [hsv_roi], [0], mask_roi, [16], [0, 180] )
             cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX);
             hist_flat = hist.reshape(-1)
             self.imgs.append(hsv)
             prob = cv2.calcBackProject(self.imgs, [0], hist_flat, [0, 180], 1)
             prob &= mask
             term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
             self.ellipse, self.bb = cv2.CamShift(prob, self.bb, term_crit)
             cv2.rectangle(self.img, (self.bb[0], self.bb[1]), (self.bb[0]+self.bb[2], self.bb[1]+self.bb[3]), color=(255,0,0))
             cv2.imshow("CAMShift", self.img)
             k = cv2.waitKey(30) % 0x100
             if k==27:
                 cv2.destroyAllWindows()
                 break
             if k==114:
                 cv2.destroyAllWindows()
                 self.start()
                 break
             _, self.img = self.cam.read()
         except KeyboardInterrupt:
             cv2.destroyAllWindows()
             break
Example #7
0
    def track(self, frame):
        
        for i in range(len(self.movingObjects)):
			
            track_window = self.movingObjects[i]
			#track_window = (c,r,w,h)
            c = track_window[0]
            r = track_window[1]
            w = track_window[2]
            h = track_window[3]
            
            if w == 0 or h == 0:
                continue
            
            #mask = cv2.inRange(hsv_roi, np.array((0., 0.,0.)), np.array((180.,255.,255.)))
            roi_hist = self.histograms[i]
            
            
        
            hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
            #dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,256],1)
            dst = cv2.calcBackProject([hsv],[0,1],roi_hist,[0,180,0,256],1)

            #ret, self.movingObjects[i] = cv2.meanShift(dst, track_window, self.term_crit)
            # apply meanshift to get the new location
            ret, self.movingObjects[i] = cv2.CamShift(dst, track_window, self.term_crit)
            
            
            # Draw it on image
            #pts = cv2.boxPoints(ret)
            #pts = np.int0(pts)
            #self.movingObjects.append(pts)
            #img2 = cv2.polylines(frame,[pts],True, 255,2)
        
        self.drawContours(frame)
def bpSignificantSquares(img,sigSquares,probThresh):
	print '-BP-'
	#get a list of the regions of the image for which the histogram for backproyection will be calculated
	print '-BP-getRegionList'
	myTime = clock()
	regionList = getSignificantRegions(img,sigSquares)
	print '-BP- len of regionList: '+str(len(regionList))
	print '-BP- ===>takes '+str(clock()-myTime)
	myTime = clock()

	mask = np.zeros((img.shape[:2]),np.uint8)

	print '-BP-gethist and bp'
	myTime = clock()
	for region in regionList:
		hist = getHist(region)[0]
		aux = cv2.calcBackProject([cv2.cvtColor(img,cv2.cv.CV_BGR2HSV)],
			[0,1], hist, [0,180,0,256],1)
		bp = cv2.threshold(aux,probThresh,255,cv2.THRESH_BINARY)[1]
		mask = cv2.max(mask,bp)
	print '-BP- ===>takes '+str(clock()-myTime)
	myTime = clock()

	aux = np.zeros((mask.shape),np.uint8)
	print '-BP-getComponentOf '
	aux = getComponentOf(mask,(mask.shape[1]/2,mask.shape[0]/2),True)
	print '-BP- ===>takes '+str(clock()-myTime)
	myTime = clock()

	return cv2.merge([mask,]*3) , cv2.merge([aux*255,]*3)
def draw_window(frame):
    # setup initial location of window
    r,h,c,w = 250,90,400,125  # simply hardcoded the values
    track_window = (c,r,w,h)    

    # set up the ROI for tracking
    roi = frame[r:r+h, c:c+w]
    hsv_roi =  cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv_roi, np.array((0., 60.,32.)), np.array((180.,255.,255.)))
    roi_hist = cv2.calcHist([hsv_roi],[0],mask,[180],[0,180])
    cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)    

    # Setup the termination criteria, either 10 iteration or move by atleast 1 pt
    term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )

    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)

    # apply meanshift to get the new location
    ret, track_window = cv2.CamShift(dst, track_window, term_crit)
    # Draw it on image
    pts = cv2.boxPoints(ret)
    pts = np.int0(pts)
    img2 = cv2.polylines(frame,[pts],True, 255,2)
    io.imshow(img2)
Example #10
0
def backproject():
        im = cv2.imread("DSC_0869.JPG")
	#im = cv2.resize(im, None, fx = 0.25, fy = 0.25)
	
	#image => hsv, hist
	hsv = cv2.cvtColor( im, cv2.COLOR_BGR2HSV)
	#cv2.imshow("hsv", hsv)
	imHist = cv2.calcHist([hsv], [0,1], None, [180, 256],[0,180,0,256])

	bckP = cv2.calcBackProject([hsv], [0,1], imHist,[0,180,0,256], 1)
	#cv2.imshow("bp", bckP)
	kernel = cv2.getStructuringElement( cv2.MORPH_ELLIPSE, (3,3))
	closing = cv2.morphologyEx(bckP, cv2.MORPH_CLOSE, kernel)
	#cv2.imshow("eroded", closing)

	##dst = cv2.filter2D(closing, -1,kernel)
	##cv2.imshow('2d', dst)

	ret,thresh = cv2.threshold(closing, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
	#cv2.imshow("thresh", thresh)

	fm1 =  cv2.merge((thresh,thresh,thresh))
	res1 = cv2.bitwise_and(im, fm1, mask = None)# mask here has no significance
        #cv2.imshow("first and", res1)
	
	#make (lower bound) G= 180 for proper target. G= 90 makes its edges disappear a leeettle
	mask = cv2.inRange(hsv, np.array([5,90,50], dtype = np.uint8), np.array([49,255,205], dtype = np.uint8)) 
	mask_inv = cv2.bitwise_not(mask)
	res = cv2.bitwise_and(res1, res1, mask = mask_inv)
	cv2.imwrite("final.jpg", res)
	kernel=cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
	res=cv2.erode(res,kernel,iterations=1)
	cv2.imwrite("bckP.JPG",res)
	extractblob(res)
    def apply_skin_mask(self, frame):
        # transform from rgb to hsv
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        # detect skin using the hand histogram
        self.skin_mask = cv2.calcBackProject([hsv], [0, 1], self.hand_histogram, [0, 180, 0, 256], 1)
        # create a elliptical kernel (12 is the best in my case)
        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11))
        cv2.filter2D(self.skin_mask, -1, kernel, self.skin_mask)
        # Apply gaussian filter on the result to give better result
        cv2.GaussianBlur(self.skin_mask, (3, 3), 0, self.skin_mask)
        # change the threshold to suit the brightness (20-30 gave me best results so far)
        _, thresh = cv2.threshold(self.skin_mask, 20, 255, 0)
        # Apply gaussian filter on the result to give better result
        cv2.GaussianBlur(self.skin_mask, (5, 5), 0, self.skin_mask)

        # Trying other types of threshold (all of them didn't work)
        # _, thresh = cv2.adaptiveThreshold(skin_mask, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, 0, 11, 2)
        # _, thresh = cv2.threshold(skin_mask, 0, 255, 0+cv2.THRESH_OTSU) #OTSU Threshold don't give satisfying results

        # Multichannel
        thresh = cv2.merge((thresh, thresh, thresh))
        # Mask the hand from the original frame
        self.skin_mask = cv2.bitwise_and(frame, thresh)
        # remove faulty skin (kernel of size 9x9)
        self.skin_mask = cv2.morphologyEx(self.skin_mask, cv2.MORPH_OPEN, (31, 31), iterations=5)
        # reduce black spaces in the hand
        cv2.morphologyEx(self.skin_mask, cv2.MORPH_CLOSE, (9, 9), self.skin_mask, iterations=5)
        # Draw Skin masking (JFD)
        if self.DEBUGGING:
            cv2.imshow('skin mask', self.skin_mask)
        return self.skin_mask
Example #12
0
    def run(self, cur_frame, next_frame,):
        # Setup the termination criteria, either 10 iteration or move by at least 1 pt
        term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
        new_list_of_objects = []

        for obj_tuple in self.list_of_objects:
            hsv_roi = None
            if len(obj_tuple) == 4:
                obj, hsv_roi, n_in_frame, n_not_moving = obj_tuple
            if (hsv_roi is not None) and (obj[2] > 0 or obj[3] > 0):
                mask = cv2.inRange(hsv_roi, np.array((0., 60., 32.)), np.array((180., 255., 255.)))
                roi_hist = cv2.calcHist([hsv_roi], [0], mask, [180], [0, 180])
                cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)

                # track in next frame
                # backprojection
                hsv = cv2.cvtColor(next_frame, cv2.COLOR_BGR2HSV)
                dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)

                # apply meanshift to get the new location
                ret, obj_new = cv2.meanShift(dst, obj, term_crit)
                n_in_frame += 1
                if PostProcessing.distance_two_squares(obj, obj_new) < 1:
                    n_not_moving += 1
                else:
                    n_not_moving = 0

                x, y, w, h = obj_new
                if n_not_moving < 20:
                    new_list_of_objects.append((obj_new, hsv_roi, n_in_frame, n_not_moving))

                # draw
                cv2.rectangle(next_frame, (x, y), (x + w, y + h), 255, 2)
        self.list_of_objects = new_list_of_objects
        pass
Example #13
0
def find_content(img_hsv, hist_sample):
    """ img hsv, hist_sample as np.array, -> 1 channel distance """
    src_img_cp = img_hsv
    # normalize the sample histogram
    cv2.normalize(hist_sample, hist_sample, 0, 179, cv2.NORM_MINMAX)
    distance = cv2.calcBackProject([img_hsv], [0], hist_sample, [0, 180], 0.5)

    print('ssssssssssssssssssssss distance -------------------')
    # show the distance
    ava.cv.utl.show_image_wait_2(distance) # ------------

    # convolute with circular, morphology
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
    cv2.filter2D(distance, -1, kernel, distance)

    print('==================== distance convoluted -------------------')
    # show the smoothed distance
    ava.cv.utl.show_image_wait_2(distance) # ------------

    # threshold
    ret, thresh = cv2.threshold(distance, 55, 180, cv2.THRESH_BINARY)
    # thresh = cv2.merge([thresh, thresh, thresh])

    # do the bitwise_and
    #result = cv2.bitwise_and(src_img_cp, thresh)
    return thresh
def sliding_window(image,r1,r2,step,roihist):
    test_path = "/home/sarbajit/PyCharm_Scripts/test/green_pad_same_name_new/final_rotated2/"
    item = image
    if item.endswith(".png") or item.endswith(".PNG"):
        x = test_path+item
        target2 = cv2.imread(x)
        target = target2[90:150, 90:520]
        (winW, winH) = (50, 30)
        for (x, y, window) in sliding_window_test(target,r1,r2,stepSize=step, windowSize=(winW, winH)):
            # if the window does not meet our desired window size, ignore it
            if window.shape[0] != winH or window.shape[1] != winW:
                continue
            #this section does the histogram backprojected matching window by window.
            hsvt = cv2.cvtColor(window, cv2.COLOR_BGR2HSV)
            inputImage = cv2.calcHist([hsvt], [0, 1], None, [180, 256], [0, 180, 0, 256])
            cv2.normalize(roihist, roihist, 0, 255, cv2.NORM_MINMAX)
            dst = cv2.calcBackProject([hsvt], [0, 1], roihist, [0, 180, 0, 256], 1)
            match = cv2.compareHist(roihist, inputImage, method=0)
            print match
            #the match is printed to see the difference and jumps when the window moves through the landing pad

            # THIS IS WHERE YOU WOULD PROCESS YOUR WINDOW,AND DO THE NECESSARY STEPS

        # we'll just draw the window and show the results
            clone = target.copy()
            cv2.rectangle(clone, (x, y), (x + winW, y + winH), (0, 255, 0), 2)
            cv2.imshow("window", clone)
            cv2.waitKey(1)
            time.sleep(1)

# sliding_window('2015-08-06_06-27-48.png',28,58,30)
    def _append_boxes_from_meanshift(self, frame, box_all):
        """Adds to the list all bounding boxes found with mean-shift tracking

            Mean-shift tracking is used to track objects from frame to frame.
            This information is combined with a saliency map to discard
            false-positives and focus only on relevant objects that move.

            :param frame: current RGB image frame
            :box_all: append bounding boxes from tracking to this list
            :returns: new list of all collected bounding boxes
        """
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

        for i in xrange(len(self.object_roi)):
            roi_hist = copy.deepcopy(self.object_roi[i])
            box_old = copy.deepcopy(self.object_box[i])

            dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)
            ret, box_new = cv2.meanShift(dst, tuple(box_old), self.term_crit)
            self.object_box[i] = copy.deepcopy(box_new)

            # discard boxes that don't move
            (xo, yo, wo, ho) = box_old
            (xn, yn, wn, hn) = box_new

            co = [xo + wo/2, yo + ho/2]
            cn = [xn + wn/2, yn + hn/2]
            if (co[0]-cn[0])**2 + (co[1]-cn[1])**2 >= self.min_shift2:
                box_all.append(box_new)

        return box_all
def trackFace(allRoiPts, allRoiHist):        
    for k in range(0, TRACK):
        # read the frame and check if the frame has been read properly
        ret, frame = cap.read()
        if not ret:
            return -1;
            break
        i=0
        # convert the given frame to HSV color space
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        # for histogram of each window found, back project them on the current frame and track using CAMSHIFT
        for roiHist in allRoiHist:
            backProj = cv2.calcBackProject([hsv], [0], roiHist, [0, 180], 1)
            (r, allRoiPts[i]) = cv2.CamShift(backProj, allRoiPts[i], termination)  
            # error handling for bound exceeding
            for j in range(0,4):         
                if allRoiPts[i][j] < 0:
                    allRoiPts[i][j] = 0
            pts = np.int0(cv2.cv.BoxPoints(r))        
            # draw bounding box around the new position of the object
            cv2.polylines(frame, [pts], True, (0, 255, 255), 2)
            i = i + 1            
        # show the face on the frame
        cv2.imshow("Faces", frame)
        cv2.waitKey(1)
    return 1;
	def track(self,im):
		im_hsv = cv2.cvtColor(im,cv2.COLOR_BGR2HSV)
		track_im = cv2.calcBackProject([im_hsv],[0],self.query_hist,[0,255],1)

		track_im_visualize = track_im.copy()
		# convert to (x,y,w,h)
		track_roi = (self.last_detection[0],self.last_detection[1],self.last_detection[2]-self.last_detection[0],self.last_detection[3]-self.last_detection[1])

		# Setup the termination criteria, either 10 iteration or move by atleast 1 pt
		# this is done to plot intermediate results of mean shift
		for max_iter in range(1,10):
			term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, max_iter, 1 )
			(ret, intermediate_roi) = cv2.CamShift(track_im,track_roi,term_crit)
			if max(intermediate_roi) == 0.0 or np.isnan(ret[0]).any() or np.isnan(ret[1]).any() or np.isnan(ret[2]).any():
				# expand ROI
				track_roi = (int(track_roi[0]),int(track_roi[1]),int(ceil(track_roi[2]*1.2)),int(ceil(track_roi[3]*1.2)))
			else:
				box = cv2.cv.BoxPoints(ret)
				box = np.int0(box)
				cv2.drawContours(track_im_visualize,[box],0,max_iter/10.0,2)

		if box == None:
			self.last_detection = [0,0,frame.shape[0],frame.shape[1]]
		else:
			self.last_detection = [intermediate_roi[0],intermediate_roi[1],intermediate_roi[0]+intermediate_roi[2],intermediate_roi[1]+intermediate_roi[3]]
			self.last_box = box
		cv2.imshow("track_win",track_im_visualize)
Example #18
0
    def track(self, img, center, face_rect, det_face_hsv):
        """
        Uses mean shifting to track the users face. Only useful once
        a face has already been detected. The Process
        1) Convert the image to HSV, since we track with Hue
        2) Pull out the Hue values in the detected region, and develop a histogram
        3) Create a Back Projection of the initial image with values equal to the
            probability of that hue appearing in the facial region
        4) Use mean shifting to determine where the new face is

        :param img: BGR image from webcam
        :param center: tuple giving the normalized center of teh face
        :param face_rect: non-normalized dimensions of face rectangle (x, y, cols, rows)
        :param det_face_hsv: hsv of the most recently detected face
        :return: (new_position, rect)
        """
        # convert the original image to hsv, and pull out the face
        img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
        hue_face_hist = cv2.calcHist([det_face_hsv],[0], None, [32], [0, 180])
        cv2.normalize(hue_face_hist, hue_face_hist, 0, 255, cv2.NORM_MINMAX)
        #calculate teh back projection probabilities
        back_proj = cv2.calcBackProject([img_hsv], [0], hue_face_hist, [0, 180], 1)
        #track face using meanshift
        term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
        track_box, rect = cv2.meanShift(back_proj, face_rect, term_crit)
        #return values
        height, width, x = img.shape #rows, cols, depth
        new_position = ((rect[0] + rect[2]/2)/float(width), (rect[1] + rect[3]/2)/float(height))
        cv2.rectangle(img, (rect[0], rect[1]), (rect[0]+rect[2], rect[1]+rect[3]), 255)

        return (new_position, rect)
Example #19
0
    def rgb_analisis(self, img):
        # TODO what happend if the object is lost?
        if self.track_window == None:
            self.detect_object(img)
            if self.track_window == None:
                return None
        else:
            hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
            dst = cv2.calcBackProject([hsv], [0], self.hist_track, [0, 180], 1)

            # apply meanshift to get the new location
            # print dst
            # print self.track_window

            ret, self.track_window = cv2.CamShift(dst, self.track_window, self.track_crit)

            # Draw it on image
            pts = cv2.cv.BoxPoints(ret)
            pts = np.int0(pts)
            cv2.polylines(img, [pts], True, (0, 255, 0), 2)
            self.img = img
            # cv2.imshow('track',img)
            # cv2.waitKey(1)
            print "Object tracked"

        return self.track_window[0] + self.track_window[2] / 2
Example #20
0
def create_image_backprops(images, **kwargs):
    """
    """
    pathdir = kwargs.get("path", "")
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))

    for index, path in images["Path"].items():
        rect = images["Crop"][index]
        path = os.path.join(pathdir, path)
        im_base = open_if_path(path, hsv=True)
        im_crop = get_image_crop(im_base, rect)

        histogram = cv2.calcHist([im_crop], [0, 1], None, [180, 256], [0, 180, 0, 256])
        cv2.normalize(histogram, histogram, 0, 255, cv2.NORM_MINMAX)
        backprop = cv2.calcBackProject([im_base], [0, 1], histogram, [0, 180, 0, 256], 1)
        cv2.filter2D(backprop, -1, kernel, backprop)
        _, im_thresh = cv2.threshold(backprop, 50, 255, 0)

        im_mask = cv2.merge((im_thresh, im_thresh, im_thresh))
        im_clean = cv2.bitwise_and(im_base, im_mask)
        examine = np.vstack([im_base, im_mask, im_clean])
        cv2.imshow(path, examine)

        while cv2.waitKey(5) != ord(" "):
            pass
    cv2.destroyAllWindows()
Example #21
0
    def skin_mask(self, img, det_face_hsv, face_rect):
        """
        Create a mask of the image which returns a binary image (black and white) based
        on whether we thing a section is skin or not. We do this by analyzing the hue and
        saturation from the detected face. From this we can calculate the probability of
        any pixel in the full image occuring in the face image. Then we can filter out
        any values whose probability is below a certain threshold.

        :param img: BGR image from webcam
        :param det_face_hsv: hsv image of the face from the previous detection
        :param face_rect: non-normalized dimensions of face rectangle (left, top, cols, rows)
        :return: 2D array, black and white if pixels are thought to be skin
        """
        #Get the HSV images of the whole thing and the face
        img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
        face_left = face_rect[0]
        face_top = face_rect[1]
        face_width = face_rect[2]
        face_height = face_rect[3]
        #create a Hue-Saturation histogram of the face
        hs_face_hist = cv2.calcHist([det_face_hsv], [0,1], None, [32,32], [0, 180,0, 255])
        cv2.normalize(hs_face_hist, hs_face_hist, 0, 255, cv2.NORM_MINMAX)
        #create a Hue-Saturation BackProjection, and a mask
        #This mask ignores dark pixels < 32, and saturated pixels, <60
        hue_min, sat_min, val_min = 0.0, 32.0, 16.0
        mask = cv2.inRange(img_hsv, np.array((hue_min, sat_min, val_min)), np.array((180., 255., 255.)))
        mask_face = mask[face_top:face_top+face_height, face_left:face_left+face_width]
        masked_hs_hist = cv2.calcHist([det_face_hsv], [0,1], mask_face, [32,32], [0, 180,0, 255])
        cv2.normalize(masked_hs_hist, masked_hs_hist, 0, 255, cv2.NORM_MINMAX)
        masked_hs_prob = cv2.calcBackProject([img_hsv], [0,1], masked_hs_hist, [0, 180,0, 255],1)
        cv2.bitwise_and(masked_hs_prob, mask, dst=masked_hs_prob) #seems to lessen noise???
        thresh = 8.0 #threshold likelihood for being skin, changes a lot based on setting
        _, masked_img = cv2.threshold(masked_hs_prob, thresh, 255, cv2.CV_8U) #throw out below thresh

        return masked_img
Example #22
0
def do_camshift(vec=0):
    global frame, roiBox
    
    # convert the current frame to the HSV color space and perform mean shift
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    backProj = cv2.calcBackProject([hsv], [0], roiHist, [0, 180], 1)

    # apply cam shift to the back projection, convert the points to a bounding box, and then draw them
    (r, roiBox) = cv2.CamShift(backProj, roiBox, termination)
    pts = np.int0(cv2.boxPoints(r))
    cv2.polylines(frame, [pts], True, (0,255,0), 2)

    centerScr = get_centerScreen(frame)
    x = centerScr[0]
    y = centerScr[1]
    
    # print "+" in the center of screen
    cv2.line(frame,(x,y-10),(x,y+10),(0,0,255),2) # print line
    cv2.line(frame,(x-10,y),(x+10,y),(0,0,255),2) # print line
   # cv2.line(frame,(x,y),(x+80,y),(0,255,0),2) # print line
    
    
    centerRoi = get_centerRoi(pts)    
    x = centerRoi[0]
    y = centerRoi[1]
    
    # print "+" in the center of ROI
    cv2.line(frame,(x,y-10),(x,y+10),(0,255,0),2) # print line
    cv2.line(frame,(x-10,y),(x+10,y),(0,255,0),2) # print line
    
    
    gimbel_move(pts,frame,vec)
    lineRoiTocenter(centerRoi,centerScr)
    print_data(vec)
    tragetAcquired(pts)
Example #23
0
 def scan(self, frame, hsv_frame, mask):
     """Updates all the of trackers for identified objects and updates the searcher which is looking for new objects."""
     bproj = cv2.calcBackProject([hsv_frame], [0], self.hist, [0,255], 1)        
     bproj &= mask        
     for index, tracker in enumerate(self.tracking):
         original_bproj = bproj.copy()
         box, bproj, split = tracker.update(bproj)
         coords, dims, theta = box
         w,h = dims
         if split:
             self.splitTracker(tracker)
             del self.tracking[index]
             bproj = original_bproj
         if tracker.hasFound() and w > 0 and h > 0:
             cv2.ellipse(frame, box, self.averageColor, 2)
         else:
             del self.tracking[index]                
     box, bproj, split = self.searcher.update(bproj.copy())        
     if split:
         self.splitTracker(self.searcher)
         self.searcher = Tracker(self.frameDims, self.full_track_window, found = False)
     if self.searcher.hasFound(): # If searcher finds an object, start tracking that object and make a new searcher
         self.tracking.append(self.searcher)
         self.searcher = Tracker(self.frameDims, self.full_track_window, found = False)            
     return frame
    def frame_track(self, cur_frame):
        vis = cur_frame.copy()
        hsv = cv2.cvtColor(cur_frame, cv2.COLOR_BGR2HSV)
        mask = cv2.inRange(hsv, np.array((0., 60., 32.)), np.array((180., 255., 255.)))

        prob = cv2.calcBackProject([hsv], [0], self.hist, [0, 180], 1)
        prob &= mask
        term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
        # get rotated bounding box
        track_box, self.track_window = cv2.CamShift(prob, self.track_window, term_crit)
        # get bounding box
        track_rect_rotate = cv2.boxPoints(track_box)
        # get rectangle 
        track_rect = cv2.boundingRect(track_rect_rotate)

        # store them in yaml compatible format
        track_rect_yaml=track_rect

        if self.show_backproj:
            vis[:] = prob[...,np.newaxis]

        pt1 = (track_rect_yaml[0],track_rect_yaml[1])
        pt2 = (track_rect_yaml[0] + track_rect_yaml[2], track_rect_yaml[1] + track_rect_yaml[3])
        cv2.rectangle(vis, pt1, pt2, (0, 255, 0), 2)
        cv2.ellipse(vis, track_box, (0, 0, 255), 2)

        self.add_frame_to_dataset_based_on_difference(cur_frame, track_rect_yaml)
        return vis
Example #25
0
def Background_remove(img_trimmed,sample_path):
    roi = cv2.imread(sample_path)
    hsv = cv2.cvtColor(roi,cv2.COLOR_BGR2HSV)   
 
    target = img_trimmed
    hsvt = cv2.cvtColor(target,cv2.COLOR_BGR2HSV)
 
    # calculating object histogram
    roihist = cv2.calcHist([hsv],[0, 1], None, [180, 256], [0, 180, 0, 256] )
 
    # normalize histogram and apply backprojection
    cv2.normalize(roihist,roihist,0,255,cv2.NORM_MINMAX)
    dst = cv2.calcBackProject([hsvt],[0,1],roihist,[0,180,0,256],1)
 
    # Now convolute with circular disc
    disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
    cv2.filter2D(dst,-1,disc,dst)
 
    # threshold and binary AND
    ret,thresh = cv2.threshold(dst,5,255,0)
    #invert to get the object of interest
    cv2.bitwise_not(thresh,thresh)
    thresh = cv2.merge((thresh,thresh,thresh))
    res = cv2.bitwise_and(target,thresh)
 
    #res = np.vstack((target,thresh,res))
    return res
Example #26
0
 def apply_skin_mask(self, frame):
     # transform from rgb to hsv
     hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
     # detect skin using the hand histogram
     self.skin_mask = cv2.calcBackProject([hsv], [0, 1], self.hand_histogram, [0, 180, 0, 256], 1)
     # create a elliptical kernel (11 is the best in my case)
     kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11))
     cv2.filter2D(self.skin_mask, -1, kernel, self.skin_mask)
     # Apply gaussian filter to give much better result
     cv2.GaussianBlur(self.skin_mask, (3, 3), 0, self.skin_mask)
     # change the threshold to suit the brightness (20-30 gave me best results so far)
     _, thresh = cv2.threshold(self.skin_mask, 20, 255, 0)
     thresh = cv2.merge((thresh, thresh, thresh))
     # Mask the hand from the original frame
     self.skin_mask = cv2.bitwise_and(frame, thresh)
     # Apply gaussian filter to give much cleaner result
     cv2.GaussianBlur(self.skin_mask, (5, 5), 0, self.skin_mask)
     # remove faulty skin (kernel of size 9x9)
     cv2.morphologyEx(self.skin_mask, cv2.MORPH_OPEN, (31, 31), self.skin_mask, iterations=5)
     # reduce black holes in the hand
     cv2.morphologyEx(self.skin_mask, cv2.MORPH_CLOSE, (9, 9), self.skin_mask, iterations=5)
     # Show skin detection result if DEBUGGING
     if self.DEBUGGING:
         cv2.imshow('SKIN', self.skin_mask)
     return self.skin_mask
Example #27
0
def main():
    cap = cv2.VideoCapture(0)    # カメラのキャプチャー
    ret,im = cap.read()          # 最初のフレームを取得
    r,h,c,w = 200,50,400,50      # 追跡したい領域の初期設定
    h0 = 0
    roi = im[r:r+h, c:c+w]       # 追跡のためのROIを設定
    hsv_roi =  cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)                             # HSV色空間に変換
    mask = cv2.inRange(hsv_roi, np.array((0, 60,32)), np.array((180,255,255)))  # マスク画像の作成
    roi_hist = cv2.calcHist([hsv_roi],[0],mask,[180],[0,180])                   # ヒストグラムの計算
    cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)                      # ヒストグラムの正規化
    term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )      # 終了基準の設定
    while(1):
        ret ,im = cap.read()
        hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)               # HSV色空間に変換
        dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1) # バックプロジェクションの計算
        cv2.setMouseCallback("CamShift",onMouse)                # マウスイベント
        # 左クリックされた場合
        if(cx!=0):
            (c,r,w,h) = (cx-25, cy-25, 50, 50)
        track_window = (c,r,w,h)
        ret, track_window = cv2.CamShift(dst, track_window, term_crit)  # 新しい場所を取得するためにcamshiftを適用
        (x,y,w,h) = track_window
        frame = cv2.rectangle(im, (x,y), (x+w,y+h),(0,0,200),2)     # 追跡している領域の表示
        cv2.circle(im,(cx,cy),10,(220,0,0), -1)                     # 左クリックした場所を円で表示
        cv2.imshow("CamShift",im)                                   # 映像の表示

        # 任意のキーが押されたら終了
        if cv2.waitKey(10) > 0:
            cap.release()
            cv2.destroyAllWindows()
            break
def main():  
	memory = ALProxy("ALMemory", NAO_IP, NAO_PORT)
	basicVideoProcessing = BasicVideoProcessing()
	basicVideoProcessing.connectToCamera()
		
	track = (0,0,0,0)
	term = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
	
	try:
		while True:
			img = basicVideoProcessing.getImageFromCamera()
			if (img == None):
				print "Img is empty"
				break
			else:
				if basicVideoProcessing.isWindowEmpty(track):
					track = basicVideoProcessing.setWindowAsFrameSize(img )
				else:
					center = retn[1]
					memory.raiseEvent("RedBallDetectedEvent", (640-center[0], 480-center[1]))
					
				hsv_w = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
				#mask1 = cv2.inRange(hsv_w, (0,204,138), (3,210,254))
				#mask2 = cv2.inRange(hsv_w, (171,173,51), (176,209,77))
				acmask = cv2.inRange(hsv_w, np.array([110,125,61]), np.array([144,156,136]))
				acmask = cv2.erode(acmask, None, iterations=1)
				acmask = cv2.dilate(acmask, None, iterations=1)
				hist = cv2.calcHist([hsv_w],[0,1],acmask,[180,255],[0,180,0,255])
				cv2.normalize(hist,hist,0,255,cv2.NORM_MINMAX)
				dst = cv2.calcBackProject([hsv_w],[0,1],hist,[0,180,0,255],1)
				
				retn, track = cv2.CamShift(dst, track, term)
				
	except BaseException, err:
		print "Undefined error: " + str(err)
Example #29
0
def run_main():
    cap = cv2.VideoCapture('test.mp4')
    ret, frame = cap.read()
    c,r,w,h = 200,250,70,70
    track_window = (c,r,w,h)

    roi = frame[r:r+h, c:c+w]
    hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv_roi, np.array((0., 30.,32.)), np.array((180.,255.,255.)))
    roi_hist = cv2.calcHist([hsv_roi], [0], mask, [180], [0, 180])
    cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)
    term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 80, 1)
    
    while True:
        ret, frame = cap.read()
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        dst = cv2.calcBackProject([hsv], [0], roi_hist, [0,180], 1)
        ret, track_window = cv2.meanShift(dst, track_window, term_crit)
        x,y,w,h = track_window
        # print track_window
        cv2.rectangle(frame, (x,y), (x+w,y+h), 255, 2)
        cv2.putText(frame, 'Tracked', (x-25,y-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2)
        cv2.imshow('Tracking', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'): break

    cap.release()
    cv2.destroyAllWindows()
    def find_center(self,im):
        '''actually do the tracking!'''
        im_hsv = cv2.cvtColor(im,cv2.COLOR_BGR2HSV)
        track_im = cv2.calcBackProject([im_hsv],[0],self.query_hist,[0,255],1)

        track_im_visualize = track_im.copy()
        # convert to (x,y,w,h)
        track_roi = (self.last_detection[0],self.last_detection[1],self.last_detection[2]-self.last_detection[0],self.last_detection[3]-self.last_detection[1])
        # Setup the termination criteria, either 10 iteration or move by atleast 1 pt
        # this is done to plot intermediate results of mean shift
        for max_iter in range(1,10):
            term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, max_iter, 1 )
            (ret, intermediate_roi) = cv2.meanShift(track_im,track_roi,term_crit)
            cv2.rectangle(track_im_visualize,(intermediate_roi[0],intermediate_roi[1]),(intermediate_roi[0]+intermediate_roi[2],intermediate_roi[1]+intermediate_roi[3]),max_iter/10.0,2)

        self.last_detection = [intermediate_roi[0],intermediate_roi[1],intermediate_roi[0]+intermediate_roi[2],intermediate_roi[1]+intermediate_roi[3]]
        
        # update_hist = True
        # if update_hist:
        #     self.query_img = im
        #     self.query_roi = intermediate_roi
        #     self.get_query_histogram()


        #get the center of the box
        posX = (self.last_detection[0]+self.last_detection[2])/2
        posY = (self.last_detection[1]+self.last_detection[3])/2

        cv2.circle(im,(posX,posY),2,(255,0,0),10)
        cv2.imshow('image',im)
        cv2.rectangle(self.query_img,(self.query_roi[0],self.query_roi[1]),(self.query_roi[0]+self.query_roi[2],self.query_roi[1]+self.query_roi[3]),1.0,2)
        cv2.imshow('query_img',self.query_img)
        cv2.waitKey(20)

        return Target(x = posX, y = posY, x_img_size = self.query_img.shape[0],y_img_size = self.query_img.shape[1])
Example #31
0
    def face_track(self):
        cap = cv2.VideoCapture(0)
        cap.set(cv2.CAP_PROP_AUTOFOCUS, 0)
        roi_hist = None
        t = 0
        while (True):
            ret, frame = cap.read()
            if ret == False:
                break
            if roi_hist is None:
                if t == 3:
                    roiPts, num_face = self.detect_face(frame)
                    if num_face != 0:
                        roi = frame[roiPts[1]:roiPts[1] + roiPts[3],
                                    roiPts[0]:roiPts[0] + roiPts[2]]
                        hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
                        roi_hist = cv2.calcHist([hsv_roi], [0, 1], None,
                                                [180, 256], [0, 180, 0, 256])
                        cv2.normalize(roi_hist, roi_hist, 0, 255,
                                      cv2.NORM_MINMAX)
                    t = 0
                else:
                    t += 1
            else:  # Face Track On
                # CamShift
                self.face_x = roiPts[0] + roiPts[2] / 2
                for i, sect in enumerate(self.face_section):
                    if self.face_x >= sect and self.face_x <= self.face_section[
                            i + 1]:
                        # self.current_face_section = i
                        self.set_face_section(i)
                        break
                # print(f"x:{self.face_x}")
                print(self.current_face_section)

                roi_w = roiPts[2]
                roi_h = roiPts[3]
                scaled_roi_w = int(roi_w * 1.5)
                scaled_roi_h = int(roi_h * 1.5)
                n_x = int(roiPts[0] - 0.25 * roi_w)
                n_y = int(roiPts[1] - 0.25 * roi_h)
                if n_x < 0:
                    n_x = 0
                if n_y < 0:
                    n_y = 0
                if n_x + scaled_roi_w > 1280:
                    n_x_2 = 1280
                else:
                    n_x_2 = n_x + scaled_roi_w

                if n_y + scaled_roi_h > 720:
                    n_y_2 = 720
                else:
                    n_y_2 = n_y + scaled_roi_h

                scaled_roiPts = [n_x, n_y, n_x_2, n_y_2]

                # Change to HSV Colour
                hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

                # Histogram BackProjection
                dst = cv2.calcBackProject([hsv], [0, 1], roi_hist,
                                          [0, 180, 0, 256], 1)
                roiPts = [n_x, n_y, scaled_roi_w, scaled_roi_h]
                ret, roiPts = cv2.CamShift(dst, roiPts, term_crit)
                pts = cv2.boxPoints(ret)
                pts = np.int0(pts)

                if self.pts_in_area(pts, area) is False:
                    roi_hist = None
                    t = 0
                    face_x = -1
                    self.set_face_section(0)
                # else:
                #     cv2.polylines(frame, [pts], True, (0,255,255), 2)
            # cv2.imshow('frame', frame)
            k = cv2.waitKey(60) & 0xff
            if k == ord('q'):
                break
        cv2.destroyAllWindows()
        cap.release()
        return
Example #32
0
        img_hsv = cv.cvtColor(img_color, cv.COLOR_BGR2HSV)
        img_ROI = img_hsv[start_y:end_y, start_x:end_x]
        cv.imshow("ROI", img_ROI)
        objectHistogram = cv.calcHist([img_ROI], [0], None, [180], (0, 180))

        cv.normalize(objectHistogram,
                     objectHistogram,
                     alpha=0,
                     beta=255,
                     norm_type=cv.NORM_MINMAX)

        step = step + 1

    elif step == 4:
        img_hsv = cv.cvtColor(img_color, cv.COLOR_BGR2HSV)
        bp = cv.calcBackProject([img_hsv], [0], objectHistogram, [0, 180], 1)

        rotatedRect, track_window = cv.CamShift(
            bp, track_window,
            (cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 1))
        # 오브젝트 위치에 빨간색 타원을 그려줌
        cv.ellipse(img_color, rotatedRect, (0, 0, 255), 2)

        pts = cv.boxPoints(rotatedRect)
        pts = np.int0(pts)

        for i in range(4):
            cv.line(img_color, tuple(pts[i]), tuple(pts[(i + 1) % 4]),
                    (0, 255, 0), 2)

    cv.imshow('color', img_color)
Example #33
0
        """录制新的手势(训练集)"""
        # saveImg = not saveImg # True
        if gesturename != '':  #
            saveImg = True
        else:
            print("Enter a gesture group name first, by enter press 'n'! ")
            saveImg = False
    elif key == ord('n'):
        # 开始录制新手势
        # 首先输入文件夹名字
        gesturename = (input("enter the gesture folder name: "))
        os.makedirs(gesturename)
        path = "./" + gesturename + "/"  # 生成文件夹的地址  用来存放录制的手势
    if search_model == True:
        frame_copy = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
        dst = cv.calcBackProject([frame_copy], [0, 1, 2], hist,
                                 [0, 180, 0, 256, 0, 256], 1)
        cv.imshow('frame2', dst)
    #展示处理之后的视频帧
    cv.imshow('frame', frame)
    #ROI显示
    cv.imshow("ROI", frame[y0:y0 + height, x0:x0 + width])

#最后记得释放捕捉
cap.release()
cv.destroyAllWindows()

#工具函数
# 显示ROI为二值模式
# 图像的二值化,就是将图像上的像素点的灰度值设置为0或255,
# 也就是将整个图像呈现出明显的只有黑和白的视觉效果。
import cv2
import numpy as np

#1
src = cv2.imread('../lena.jpg')
hsv = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)

#2
roi = cv2.selectROI(src)
print('roi=', roi)
roi_h = h[roi[1]:roi[1] + roi[3], roi[0]:roi[0] + roi[2]]
hist = cv2.calcHist([roi_h], [0], None, [64], [0, 256])
backP = cv2.calcBackProject([h.astype(np.float32)], [0],
                            hist, [0, 256],
                            scale=1.0)

#3
hist = cv2.sort(hist, cv2.SORT_EVERY_COLUMN + cv2.SORT_DESCENDING)
k = 1
T = hist[k][0] - 1
print('T=', T)
ret, dst = cv2.threshold(backP, T, 255, cv2.THRESH_BINARY)

cv2.imshow('dst', dst)
cv2.waitKey()
cv2.destroyAllWindows()
Example #35
0
    def camShiftTracking(self, roi, roiMask, image, imageStream, trackWindow):
        # Termination criteria
        termCrit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 3, 1)

        self.trackWindow = trackWindow

        roiHSV = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)

        # Histogram backprojection
        roiHist = cv2.calcHist([roiHSV], [0], roiMask, [16], [0, 180])
        roiHist = cv2.normalize(roiHist, roiHist, 0, 255, cv2.NORM_MINMAX)

        # initial error is zero (frames where tarps are not detected)
        error = 0

        while error < 8:
            # Get the next image in the image stream
            ret, image = imageStream.read()

            # Check to see if image is not NoneType
            if ret == True:
                # Get the HSV image
                imageHSV = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

                dst = cv2.calcBackProject([imageHSV], [0], roiHist, [0, 180],
                                          1)

                # Find new tracking window
                ret, self.trackWindow = cv2.CamShift(dst, self.trackWindow,
                                                     termCrit)

                points = cv2.boxPoints(ret)
                points = np.int0(points)

                if points[0] is [0, 0]:
                    continue

                imageCAMShift = cv2.polylines(image, [points], True, 255, 1)

                # New window of analysis
                windowOfAnalysis = self.getWindow(points)

                # Define new region of interest
                roiNew = image[windowOfAnalysis[2]:windowOfAnalysis[3],
                               windowOfAnalysis[0]:windowOfAnalysis[1]]

                # check if tarps are found
                tarpsFound = self.findTarps(image, roiNew, windowOfAnalysis)

                # Updating error count
                if not tarpsFound:
                    error += 1
                else:
                    cv2.imshow("image", image)
                    if error > 0:
                        error -= 1

            else:
                error += 1

            if error == 4:
                break

            if cv2.waitKey(1) & 0xFF is 27:
                break
Example #36
0
def predictor():
	global prediction
	cam = cv2.VideoCapture(1)
	if cam.read()[0] == False:
		cam = cv2.VideoCapture(0)
	hist = grid()
	x, y, w, h = 300, 100, 300, 300
	while True:
		text = ""
		img = cam.read()[1]
		img = cv2.flip(img, 1)
		img = cv2.resize(img, (640, 480))
		imgCrop = img[y : y + h, x : x + w]
		imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
		dst = cv2.calcBackProject([imgHSV], [0, 1], hist, [0, 180, 0, 256], 1)
		disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(10, 10))
		dst = cv2.filter2D(dst,-1,disc,dst)
		blur = cv2.GaussianBlur(dst, (11,11), 0)
		blur = cv2.medianBlur(blur, 15)
		#blur = cv2.bilateralFilter(blur, 9, 75, 75)
		thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
		thresh = cv2.merge((thresh,thresh,thresh))
		thresh = cv2.cvtColor(thresh, cv2.COLOR_BGR2GRAY)
		thresh = thresh[y : y + h, x : x + w]
#		kernel = np.ones((3, 3), np.uint8)
#		opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations = 2)

#		#sure background area
#		sureBackground = cv2.dilate(opening, kernel, iterations = 3)

#		#sure foreground area
#		distanceTransform = cv2.distanceTransform(opening, cv2.DIST_L2, 5)
#		_, sureForeground = cv2.threshold(distanceTransform, 0.7 * distanceTransform.max(), 255, 0)

#		#unknown region
#		sureForeground = np.uint8(sureForeground)
#		unknown = cv2.subtract(sureBackground, sureForeground)
#		known = cv2.subtract(sureForeground, sureBackground)

		contours = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[0]
		if len(contours) > 0:
			contour = max(contours, key = cv2.contourArea)
			#print(cv2.contourArea(contour))
			if cv2.contourArea(contour) > 10000:
				x1, y1, w1, h1 = cv2.boundingRect(contour)
				save_img = thresh[y1 : y1 + h1, x1 : x1 + w1]
				
				if w1 > h1:
					save_img = cv2.copyMakeBorder(save_img, int((w1 - h1) / 2) , int((w1 - h1) / 2) , 0, 0, cv2.BORDER_CONSTANT, (0, 0, 0))
				elif h1 > w1:
					save_img = cv2.copyMakeBorder(save_img, 0, 0, int((h1 - w1) / 2) , int((h1 - w1) / 2) , cv2.BORDER_CONSTANT, (0, 0, 0))
				
				pred_probability, pred_class = probability(model, save_img)
				
				if pred_probability * 100 > 80:
					text = class_from_db(pred_class)
					print(text)
		board = np.zeros((480, 640, 3), dtype = np.uint8)
		arr = split_to_array(text, 2)
		print_text(board, arr)
		cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 0), 2)
		res = np.hstack((img, board))
		cv2.imshow("recognizing gesture...", res)
		cv2.imshow("thresh", thresh)
		if cv2.waitKey(1) == ord('q'):
			break
Example #37
0
def recognize():
    global prediction
    cam = cv2.VideoCapture(1)
    if cam.read()[0] == False:
        cam = cv2.VideoCapture(0)
    hist = get_hand_hist()
    x, y, w, h = 300, 100, 300, 300
    while True:
        text = ""
        img = cam.read()[1]
        img = cv2.flip(img, 1)
        img = cv2.resize(img, (640, 480))
        imgCrop = img[y:y + h, x:x + w]
        imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
        dst = cv2.calcBackProject([imgHSV], [0, 1], hist, [0, 180, 0, 256], 1)
        disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10))
        cv2.filter2D(dst, -1, disc, dst)
        blur = cv2.GaussianBlur(dst, (11, 11), 0)
        blur = cv2.medianBlur(blur, 15)
        thresh = cv2.threshold(blur, 0, 255,
                               cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
        thresh = cv2.merge((thresh, thresh, thresh))
        thresh = cv2.cvtColor(thresh, cv2.COLOR_BGR2GRAY)
        thresh = thresh[y:y + h, x:x + w]
        contours = cv2.findContours(thresh.copy(), cv2.RETR_TREE,
                                    cv2.CHAIN_APPROX_NONE)[1]
        contours = cv2.findContours(thresh.copy(), cv2.RETR_TREE,
                                    cv2.CHAIN_APPROX_NONE)[0]
        if len(contours) > 0:
            contour = max(contours, key=cv2.contourArea)
            #print(cv2.contourArea(contour))
            if cv2.contourArea(contour) > 10000:
                x1, y1, w1, h1 = cv2.boundingRect(contour)
                save_img = thresh[y1:y1 + h1, x1:x1 + w1]

                if w1 > h1:
                    save_img = cv2.copyMakeBorder(save_img, int((w1 - h1) / 2),
                                                  int((w1 - h1) / 2), 0, 0,
                                                  cv2.BORDER_CONSTANT,
                                                  (0, 0, 0))
                elif h1 > w1:
                    save_img = cv2.copyMakeBorder(save_img, 0, 0,
                                                  int((h1 - w1) / 2),
                                                  int((h1 - w1) / 2),
                                                  cv2.BORDER_CONSTANT,
                                                  (0, 0, 0))

                pred_probab, pred_class = keras_predict(model, save_img)

                if pred_probab * 100 > 80:
                    text = get_pred_text_from_db(pred_class)
                    print(text)
        blackboard = np.zeros((480, 640, 3), dtype=np.uint8)
        splitted_text = split_sentence(text, 2)
        put_splitted_text_in_blackboard(blackboard, splitted_text)
        #cv2.putText(blackboard, text, (30, 200), cv2.FONT_HERSHEY_TRIPLEX, 1.3, (255, 255, 255))
        cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
        res = np.hstack((img, blackboard))
        cv2.imshow("Recognizing gesture", res)
        cv2.imshow("thresh", thresh)
        if cv2.waitKey(1) == ord('q'):
            break
Example #38
0
    # Read a new frame
    ok, frame = video.read()

    if not ok:
        break

    # Start timer
    timer = cv2.getTickCount()
    frame = cv2.resize(frame, (video_w, video_h))

    frame2 = frame.copy()

    # -------- meanshift -------------

    hsv_frame = cv2.cvtColor(frame.copy(), cv2.COLOR_BGR2HSV)
    mean_dst = cv2.calcBackProject([hsv_frame], [0], roi_hist, [0, 180], 1)

    cv2.imshow('mean_dst', mean_dst)

    # apply meanshift to get the new location
    ret, track_window = cv2.meanShift(mean_dst, track_window, term_crit)

    # Draw it on image
    xm, ym, wm, hm = track_window
    cv2.rectangle(frame, (xm, ym), (xm + wm, ym + hm), (0, 255, 255), 2, 1)

    # -------- meanshift end ---------

    # -------- fgbg ------------------
    # frame2 = cv2.GaussianBlur(frame2, (3, 3), 0)
    #
Example #39
0
def get_hand_hist():
    cam = cv2.VideoCapture(1)
    if cam.read()[0] == False:
        cam = cv2.VideoCapture(0)
    x, y, w, h = 300, 100, 300, 300
    flagPressedC, flagPressedS, flagPressedQ = False, False, False

    # img切割的範圍
    imgCrop = None
    while True:

        # 搜尋目標圖片
        img = cam.read()[1]
        img = cv2.flip(img, 1)
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
        cv2.imshow("hsv", hsv)

        keypress = cv2.waitKey(1)
        if keypress == ord('c'):

            # 擷取imgCrop圖片的HSV
            hsvCrop = cv2.cvtColor(imgCrop, cv2.COLOR_BGR2HSV)
            # print(hsvCrop.shape)
            flagPressedC = True

            # region cv2.calcHist用法
            # cv2.calcHist(images, channels, mask, histSize, ranges)
            # imaages:要分析的圖片檔
            # channels:產生的直方圖類型。例:[0]→灰階,[0, 1, 2]→RGB三色。
            # mask:optional,若有提供則僅計算mask的部份。
            # histSize:要切分的像素強度值範圍,預設為256。每個channel皆可指定一個範圍。例如,[32,32,32] 表示RGB三個channels皆切分為32區段。
            # ranges:X軸(像素強度)的範圍,預設為[0,256](非筆誤,calcHist函式的要求,最後那個值是表示<256)。
            # endregion

            # 用hsvCrop抓取所需的直方圖範圍,藉此追蹤手部
            hist = cv2.calcHist([hsvCrop], [0, 1], None, [60, 140],
                                [0, 180, 0, 256])
            # hist = cv2.calcHist([hsvCrop], [0, 1], None, [180, 256], [0, 180, 0, 256]) origin

            # 正規化 結果在0~1之間
            cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)
        elif keypress == ord('s'):
            flagPressedS = True
            break
        elif keypress == ord('q'):
            flagPressedQ = True
            break
        if flagPressedC:
            # 直方圖反向投影  利用直方圖顯示感興趣的區塊
            dst = cv2.calcBackProject([hsv], [0, 1], hist, [0, 180, 0, 256], 1)
            cv2.imshow("dst", dst)

            # 複製
            dst1 = dst.copy()

            # 定義結構元素
            disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (14, 14))
            # disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(10,10)) origin

            # region cv2.filter2D用法
            # cv2.filter2D(src, ddepth, kernel, dst, anchor)
            # src:輸入圖。
            # dst:輸出圖,和輸入圖的尺寸、通道數相同。
            # ddepth:輸出圖深度。
            # kernel:使用的核心。
            # anchor:錨點,預設為核心中央
            # endregion

            # 濾波(filtering) 提取感興趣的視覺特徵
            cv2.filter2D(dst, -1, disc, dst)

            # 高斯模糊
            blur = cv2.GaussianBlur(dst, (15, 15), 0)
            cv2.imshow("blur", blur)

            # 中位數模糊
            blur = cv2.medianBlur(blur, 15)

            # 二值化
            ret, thresh = cv2.threshold(blur, 0, 255,
                                        cv2.THRESH_BINARY + cv2.THRESH_OTSU)

            # 將 "多個單通道" 圖像合併成 "一個多通道圖像"
            thresh = cv2.merge((thresh, thresh, thresh))
            cv2.imshow("Thresh", thresh)

        if not flagPressedS:
            imgCrop = build_squares(img)
        cv2.imshow("Set hand histogram", img)
    cam.release()
    cv2.destroyAllWindows()

    # 開啟一個檔名"hist"的檔案
    if not flagPressedQ:
        with open("hist", "wb") as f:
            # 將"hist"寫入
            pickle.dump(hist, f)
Example #40
0
def get_hand_hist():
    cam = cv2.VideoCapture(0)
    host = "192.168.0.77:8080"
    if len(sys.argv) > 1:
        host = sys.argv[1]
    hoststr = 'http://' + host + '/video'
    print('Streaming ' + hoststr)
    stream = urllib.request.urlopen(hoststr)
    bytes = b''

    x, y, w, h = 300, 100, 300, 300
    flagPressedC, flagPressedS = False, False
    imgCrop = None
    while True:

        bytes += stream.read(1024)
        a_temp = b'\xff\xd8'
        b_temp = b'\xff\xd9'
        a = bytes.find(a_temp)
        b = bytes.find(b_temp)
        if a != -1 and b != -1:
            jpg = bytes[a:b + 2]
            bytes = bytes[b + 2:]
            img = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),
                               cv2.IMREAD_COLOR)

            img = cv2.resize(img, (640, 480))

            #img = cam.read()[1]
            img = cv2.flip(img, 1)
            height, width = img.shape[:2]
            hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

            keypress = cv2.waitKey(1)
            if keypress == ord('c'):
                hsvCrop = cv2.cvtColor(imgCrop, cv2.COLOR_BGR2HSV)
                flagPressedC = True
                hist = cv2.calcHist([hsvCrop], [0, 1], None, [180, 256],
                                    [0, 180, 0, 256])
                cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)
            elif keypress == ord('s'):
                flagPressedS = True
                break
            if flagPressedC:
                dst = cv2.calcBackProject([hsv], [0, 1], hist,
                                          [0, 180, 0, 256], 1)
                disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10))
                cv2.filter2D(dst, -1, disc, dst)
                blur = cv2.GaussianBlur(dst, (11, 11), 0)
                blur = cv2.medianBlur(blur, 15)
                ret, thresh = cv2.threshold(
                    blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
                thresh = cv2.merge((thresh, thresh, thresh))
                res = cv2.bitwise_and(img, thresh)
                #cv2.imshow("res", res)
                cv2.imshow("Thresh", thresh)
            if not flagPressedS:
                imgCrop = build_squares(img)
            #cv2.rectangle(img, (x,y), (x+w, y+h), (0,255,0), 2)
            cv2.imshow("Set hand histogram", img)
    cam.release()
    cv2.destroyAllWindows()
    with open("hist", "wb") as f:
        pickle.dump(hist, f)
Example #41
0
    def __init__(self, queue):
        # construct the argument parse and parse the arguments
        ap = argparse.ArgumentParser()
        ap.add_argument("-v",
                        "--video",
                        help="path to the (optional) video file")
        args = vars(ap.parse_args())

        # grab the reference to the current frame, list of ROI
        # points and whether or not it is ROI selection mode
        self.frame = None
        self.roiPts1 = []
        self.roiPts2 = []
        self.inputMode = False
        self.Detect = False

        self.MedQueueX1 = collections.deque([])
        self.MedQueueX2 = collections.deque([])
        self.MedQueueY1 = collections.deque([])
        self.MedQueueY2 = collections.deque([])

        self.MedNum = 50
        # if the video path was not supplied, grab the reference to the
        # camera
        if not args.get("video", False):
            camera = cv2.VideoCapture(0)

        # otherwise, load the video
        else:
            camera = cv2.VideoCapture(args["video"])

        # setup the mouse callback
        cv2.namedWindow("frame")
        #cv2.setMouseCallback("frame", self.selectROI)

        # initialize the termination criteria for cam shift, indicating
        # a maximum of ten iterations or movement by a least one pixel
        # along with the bounding box of the ROI
        termination = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 20, 1)

        roiBox1 = None
        roiBox2 = None

        # keep looping over the frames
        while True:
            # grab the current frame

            (grabbed, self.frame) = camera.read()

            # check to see if we have reached the end of the
            # video
            if not grabbed:
                break

            # if the see if the ROI has been computed
            if roiBox1 is not None and roiBox2 is not None:
                # convert the current frame to the HSV color space
                # and perform mean shift
                hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV)

                backProj1 = cv2.calcBackProject([hsv], [1], roiHist1,
                                                [140, 256], 1)
                backProj2 = cv2.calcBackProject([hsv], [1], roiHist2,
                                                [140, 256], 1)

                # apply cam shift to the back projection, convert the
                # points to a bounding box, and then draw them
                (r1, roiBox1) = cv2.CamShift(backProj1, roiBox1, termination)
                (r2, roiBox2) = cv2.CamShift(backProj2, roiBox2, termination)
                pts1 = np.int0(cv2.boxPoints(r1))
                pts2 = np.int0(cv2.boxPoints(r2))
                cv2.polylines(self.frame, [pts1], True, (0, 255, 0), 2)
                cv2.polylines(self.frame, [pts2], True, (0, 255, 0), 2)

                center_x1 = (pts1[0][0] + pts1[1][0] + pts1[2][0] +
                             pts1[3][0]) / 4
                center_y1 = (pts1[0][1] + pts1[1][1] + pts1[2][1] +
                             pts1[3][1]) / 4

                center_x2 = (pts2[0][0] + pts2[1][0] + pts2[2][0] +
                             pts2[3][0]) / 4
                center_y2 = (pts2[0][1] + pts2[1][1] + pts2[2][1] +
                             pts2[3][1]) / 4

                if len(self.MedQueueX1) == self.MedNum:
                    self.MedQueueX1.popleft()
                    self.MedQueueX2.popleft()
                    self.MedQueueY1.popleft()
                    self.MedQueueY2.popleft()

                self.MedQueueX1.append(center_x1)
                self.MedQueueX2.append(center_x2)
                self.MedQueueY1.append(center_y1)
                self.MedQueueY2.append(center_y2)

                if len(self.MedQueueX1) > 0:
                    med_x1 = np.median(self.MedQueueX1)
                    med_x2 = np.median(self.MedQueueX2)
                    med_y1 = np.median(self.MedQueueY1)
                    med_y2 = np.median(self.MedQueueY2)

                queue.put((1, med_x1, med_y1))
                queue.put((2, med_x2, med_y2))
                print("CamShift Player 1 X is %d, Y is %d" %
                      (center_x1, center_y1))
                print("CamShift Player 2 X is %d, Y is %d" %
                      (center_x2, center_y2))

            # show the frame and record if the user presses a key
            key = cv2.waitKey(1) & 0xFF
            cv2.imshow("frame", self.frame)

            if not self.Detect:
                orig = self.frame.copy()
                #cv2.imshow("frame", self.frame)
                x, y, r = self.detection(self.frame)
                if not (r == []):
                    # The condition states that we wait until exactly 2 players are found.
                    # Built the range of interest(roi) as the square that blocks the Hough circle.
                    self.roiPts1 = [(int(x[0] - r[0]), int(y[0] - r[0])),
                                    (int(x[0] + r[0]), int(y[0] - r[0])),
                                    (int(x[0] - r[0]), int(y[0] + r[0])),
                                    (int(x[0] + r[0]), int(y[0] + r[0]))]
                    self.roiPts2 = [(int(x[1] - r[1]), int(y[1] - r[1])),
                                    (int(x[1] + r[1]), int(y[1] - r[1])),
                                    (int(x[1] - r[1]), int(y[1] + r[1])),
                                    (int(x[1] + r[1]), int(y[1] + r[1]))]
                    self.roiPts1 = np.array(self.roiPts1)
                    self.roiPts2 = np.array(self.roiPts2)

                    # maxY = np.shape(orig)(3)

                    self.roiPts1[self.roiPts1 < 0] = 0
                    self.roiPts2[self.roiPts2 < 0] = 0
                    # self.roiPts1[self.roiPts1 > maxY] = maxY
                    # self.roiPts2[self.roiPts2 > maxY] = maxY
                    print("ROI PTS")
                    print(self.roiPts1)
                    print(self.roiPts2)

                    s1 = self.roiPts1.sum(axis=1)
                    s2 = self.roiPts2.sum(axis=1)
                    tl1 = self.roiPts1[np.argmin(s1)]
                    tl2 = self.roiPts2[np.argmin(s2)]
                    br1 = self.roiPts1[np.argmax(s1)]
                    br2 = self.roiPts2[np.argmax(s2)]

                    # grab the ROI for the bounding box and convert it
                    # to the HSV color space
                    roi1 = orig[tl1[1]:br1[1], tl1[0]:br1[0]]
                    roi1 = cv2.cvtColor(roi1, cv2.COLOR_BGR2HSV)
                    roi2 = orig[tl2[1]:br2[1], tl2[0]:br2[0]]
                    roi2 = cv2.cvtColor(roi2, cv2.COLOR_BGR2HSV)

                    print("ROI1")
                    print(roi1)
                    print("ROI2")
                    print(roi2)
                    # compute a HSV histogram for the ROI and store the
                    # bounding box
                    roiHist1 = cv2.calcHist([roi1], [1], None, [256],
                                            [140, 255], False)
                    roiHist1 = cv2.normalize(roiHist1, roiHist1, 0, 255,
                                             cv2.NORM_MINMAX)
                    roiBox1 = (tl1[0], tl1[1], br1[0], br1[1])
                    roiHist2 = cv2.calcHist([roi2], [1], None, [256],
                                            [140, 255], False)
                    roiHist2 = cv2.normalize(roiHist2, roiHist2, 0, 255,
                                             cv2.NORM_MINMAX)
                    roiBox2 = (tl2[0], tl2[1], br2[0], br2[1])

                    self.Detect = True

            # if the 'q' key is pressed, stop the loop
            elif key == ord("q"):
                break

            #queue.put((0,0,0))

        # cleanup the camera and close any open windows
        camera.release()
        cv2.destroyAllWindows()
Example #42
0
import cv2
import numpy as np

roi = cv2.imread('federerElem.jpg')
print(roi.shape)
hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)

target = cv2.imread('federer_resize.jpg')
hsvt = cv2.cvtColor(target, cv2.COLOR_BGR2HSV)

# calculating object histogram
roihist = cv2.calcHist([hsv], [0, 1], None, [180, 256], [0, 180, 0, 256])

# normalize histogram and apply backprojection
cv2.normalize(roihist, roihist, 0, 255, cv2.NORM_MINMAX)
dst = cv2.calcBackProject([hsvt], [0, 1], roihist, [0, 180, 0, 256], 1)

# Now convolute with circular disc
disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
cv2.filter2D(dst, -1, disc, dst)

# threshold and binary AND
ret, thresh = cv2.threshold(dst, 50, 255, 0)
thresh = cv2.merge((thresh, thresh, thresh))
res = cv2.bitwise_and(target, thresh)

res = np.vstack((target, thresh, res))
#cv2.imwrite('res.jpg',res)
cv2.imshow('res', res)
cv2.imshow('Elemnt', roi)
cv2.waitKey(0)
Example #43
0
def main(args):
    #Clean previous image
    clean_images()
    #Training phase
    model = training()

    vidcap = cv2.VideoCapture(args.file_name)

    fps = vidcap.get(cv2.CAP_PROP_FPS)
    width = vidcap.get(3)  # float
    height = vidcap.get(4)  # float

    # Define the codec and create VideoWriter object
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter(dir_path + "\outputs\output.avi", fourcc, fps,
                          (640, 480))

    # initialize the termination criteria for cam shift, indicating
    # a maximum of ten iterations or movement by a least one pixel
    # along with the bounding box of the ROI
    termination = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
    roiBox = None
    roiHist = None

    success = True
    similitary_contour_with_circle = 0.65  # parameter
    count = 0
    current_sign = None
    current_text = ""
    current_size = 0
    sign_count = 0
    coordinates = []
    position = []
    file = open(dir_path + "\Output.txt", "w")
    try:
        while True:
            success, frame = vidcap.read()
            if not success:
                print("FINISHED")
                break
            width = frame.shape[1]
            height = frame.shape[0]
            #frame = cv2.resize(frame, (640,int(height/(width/640))))
            frame = cv2.resize(frame, (640, 480))

            print("Frame:{}".format(count))
            #image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
            coordinate, image, sign_type, text = localization(
                frame, args.min_size_components,
                args.similitary_contour_with_circle, model, count,
                current_sign)
            if coordinate is not None:
                cv2.rectangle(image, coordinate[0], coordinate[1],
                              (255, 255, 255), 1)
            print("Sign:{}".format(sign_type))
            if sign_type > 0 and (not current_sign
                                  or sign_type != current_sign):
                current_sign = sign_type
                current_text = text
                # top = int(coordinate[0][1]*1.05)
                # left = int(coordinate[0][0]*1.05)
                # bottom = int(coordinate[1][1]*0.95)
                # right = int(coordinate[1][0]*0.95)
                top = int(coordinate[0][1])
                left = int(coordinate[0][0])
                bottom = int(coordinate[1][1])
                right = int(coordinate[1][0])

                position = [
                    count, sign_type if sign_type <= 8 else 8,
                    coordinate[0][0], coordinate[0][1], coordinate[1][0],
                    coordinate[1][1]
                ]
                cv2.rectangle(image, coordinate[0], coordinate[1], (0, 255, 0),
                              1)
                font = cv2.FONT_HERSHEY_PLAIN
                cv2.putText(image, text,
                            (coordinate[0][0], coordinate[0][1] - 15), font, 1,
                            (0, 0, 255), 2, cv2.LINE_4)

                tl = [left, top]
                br = [right, bottom]
                print(tl, br)
                current_size = math.sqrt(
                    math.pow((tl[0] - br[0]), 2) +
                    math.pow((tl[1] - br[1]), 2))
                # grab the ROI for the bounding box and convert it
                # to the HSV color space
                roi = frame[tl[1]:br[1], tl[0]:br[0]]
                print(roi)
                roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
                #roi = cv2.cvtColor(roi, cv2.COLOR_BGR2LAB)

                # compute a HSV histogram for the ROI and store the
                # bounding box
                roiHist = cv2.calcHist([roi], [0], None, [16], [0, 180])
                roiHist = cv2.normalize(roiHist, roiHist, 0, 255,
                                        cv2.NORM_MINMAX)
                roiBox = (tl[0], tl[1], br[0], br[1])

            elif current_sign:
                hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
                backProj = cv2.calcBackProject([hsv], [0], roiHist, [0, 180],
                                               1)

                # apply cam shift to the back projection, convert the
                # points to a bounding box, and then draw them
                (r, roiBox) = cv2.CamShift(backProj, roiBox, termination)
                pts = np.int0(cv2.boxPoints(r))
                s = pts.sum(axis=1)
                tl = pts[np.argmin(s)]
                br = pts[np.argmax(s)]
                size = math.sqrt(
                    pow((tl[0] - br[0]), 2) + pow((tl[1] - br[1]), 2))
                print(size)

                if current_size < 1 or size < 1 or size / current_size > 30 or math.fabs(
                    (tl[0] - br[0]) / (tl[1] - br[1])) > 2 or math.fabs(
                        (tl[0] - br[0]) / (tl[1] - br[1])) < 0.5:
                    current_sign = None
                    print("Stop tracking")
                else:
                    current_size = size

                if sign_type > 0:
                    top = int(coordinate[0][1])
                    left = int(coordinate[0][0])
                    bottom = int(coordinate[1][1])
                    right = int(coordinate[1][0])

                    position = [
                        count, sign_type if sign_type <= 8 else 8, left, top,
                        right, bottom
                    ]
                    cv2.rectangle(image, coordinate[0], coordinate[1],
                                  (0, 255, 0), 1)
                    font = cv2.FONT_HERSHEY_PLAIN
                    cv2.putText(image, text,
                                (coordinate[0][0], coordinate[0][1] - 15),
                                font, 1, (0, 0, 255), 2, cv2.LINE_4)
                elif current_sign:
                    position = [
                        count, sign_type if sign_type <= 8 else 8, tl[0],
                        tl[1], br[0], br[1]
                    ]
                    cv2.rectangle(image, (tl[0], tl[1]), (br[0], br[1]),
                                  (0, 255, 0), 1)
                    font = cv2.FONT_HERSHEY_PLAIN
                    cv2.putText(image, current_text, (tl[0], tl[1] - 15), font,
                                1, (0, 0, 255), 2, cv2.LINE_4)

            if current_sign:
                sign_count += 1
                coordinates.append(position)

            cv2.imshow('Result', image)
            count = count + 1
            #Write to video
            out.write(image)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
    except Exception as e:
        traceback.print_exc()
        print(e)
    file.write("{}".format(sign_count))
    for pos in coordinates:
        file.write("\n{} {} {} {} {} {}".format(pos[0], pos[1], pos[2], pos[3],
                                                pos[4], pos[5]))
    print("Finish {} frames".format(count))
    file.close()
    # ff = FFmpeg(
    #     inputs={'./outputs/output.avi': ['-y']},
    #     outputs={'./outputs/output.mp4': None}
    # )
    # ff.run()
    return
channels = [0, 1]
ranges = [0, 180, 0, 256]
hist = cv2.calcHist([roi_hsv], channels, None, [90, 128], ranges)

# Mean Shift 알고리즘 종료 기준
term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)

# 비디오 매 프레임 처리
while True:
    ret, frame = cap.read()

    if not ret:
        break

    # HS 히스토그램에 대한 역투영
    frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    backproj = cv2.calcBackProject([frame_hsv], channels, hist, ranges, 1)

    # Mean Shift
    _, rc = cv2.meanShift(backproj, rc, term_crit)

    # 추적 결과 화면 출력
    cv2.rectangle(frame, rc, (0, 0, 255), 2)
    cv2.imshow('frame', frame)

    if cv2.waitKey(60) == 27:
        break

cap.release()
cv2.destroyAllWindows()
Example #45
0
    def detect_video(self, yolo, video_path):
        from PIL import Image, ImageFont, ImageDraw
        #Start ROS node
        # pub, pub_flag, pub_track, pub_frame1, pub_frame2 = start_node()
        pub, pub_flag, pub_frame1, pub_frame2 = start_node()
        vid = RealsenseCapture()
        vid.start()
        bridge = CvBridge()

        accum_time = 0
        curr_fps = 0
        fps = "FPS: ??"
        prev_time = timer()
        worldy = 0.0
        flag = 0

        while True:
            # pub_track.publish(0)
            # flag = 0
            if self.garbage_in_can == 1:
                # print("ゴミを捨てました")
                flag = 0
            pub_flag.publish(flag)
            ret, frames, _ = vid.read()
            frame = frames[0]
            depth_frame = frames[1]
            image = Image.fromarray(frame)
            image, bottle, person, right, left, bottom, top, right2, left2, bottom2, top2 = yolo.detect_image(
                image, pub)
            result = np.asarray(image)
            curr_time = timer()
            exec_time = curr_time - prev_time
            prev_time = curr_time
            accum_time = accum_time + exec_time
            curr_fps = curr_fps + 1
            if accum_time > 1:
                accum_time = accum_time - 1
                fps = "FPS: " + str(curr_fps)
                curr_fps = 0
            cv2.putText(result,
                        text=fps,
                        org=(3, 15),
                        fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                        fontScale=0.50,
                        color=(255, 0, 0),
                        thickness=2)
            # cv2.imshow("result", result)
            yolo_frame = bridge.cv2_to_imgmsg(result, "bgr8")
            pub_frame1.publish(yolo_frame)

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

            if (bottle == False) or (person == False):
                continue

        # ------------------------------Tracking-----------------------------------
        # tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT']
        # tracker_type = tracker_types[7]
            tracker = cv2.TrackerCSRT_create()
            tracker2 = cv2.TrackerCSRT_create()

            # setup initial location of window
            left, right, top, bottom = left, right, top, bottom
            r, h, ci, w = top, bottom - top, left, right - left  # simply hardcoded the values r, h, c, w
            frame_b, frame_g, frame_r = frame[:, :, 0], frame[:, :,
                                                              1], frame[:, :,
                                                                        2]
            hist_b = cv2.calcHist([frame_b[top:bottom, left:right]], [0], None,
                                  [256], [0, 256])
            hist_g = cv2.calcHist([frame_g[top:bottom, left:right]], [0], None,
                                  [256], [0, 256])
            hist_r = cv2.calcHist([frame_r[top:bottom, left:right]], [0], None,
                                  [256], [0, 256])
            cv2.normalize(hist_b, hist_b, 0, 255, cv2.NORM_MINMAX)
            cv2.normalize(hist_g, hist_g, 0, 255, cv2.NORM_MINMAX)
            cv2.normalize(hist_r, hist_r, 0, 255, cv2.NORM_MINMAX)
            track_window = (ci, r, w, h)

            r2, h2, ci2, w2 = top2, bottom2 - top2, left2, right2 - left2  # simply hardcoded the values r, h, c, w
            hist_bp = cv2.calcHist([frame_b[top2:bottom2, left2:right2]], [0],
                                   None, [256], [0, 256])
            hist_gp = cv2.calcHist([frame_g[top2:bottom2, left2:right2]], [0],
                                   None, [256], [0, 256])
            hist_rp = cv2.calcHist([frame_r[top2:bottom2, left2:right2]], [0],
                                   None, [256], [0, 256])
            cv2.normalize(hist_bp, hist_bp, 0, 255, cv2.NORM_MINMAX)
            cv2.normalize(hist_gp, hist_gp, 0, 255, cv2.NORM_MINMAX)
            cv2.normalize(hist_rp, hist_rp, 0, 255, cv2.NORM_MINMAX)
            track_window2 = (ci2, r2, w2, h2)
            cv2.imwrite('bottledetect.jpg', frame[r:r + h, ci:ci + w])
            cv2.imwrite('persondetect.jpg', frame[r2:r2 + h2, ci2:ci2 + w2])

            # set up the ROI for tracking
            roi = frame[r:r + h, ci:ci + w]
            hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
            mask = cv2.inRange(hsv_roi, np.array((0., 60., 32.)),
                               np.array((180., 255., 255.)))
            roi_hist = cv2.calcHist([hsv_roi], [0], mask, [180], [0, 180])
            cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)

            # Setup the termination criteria, either 10 iteration or move by atleast 1 pt
            term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10,
                         1)

            ok = tracker.init(frame, track_window)
            ok2 = tracker2.init(frame, track_window2)

            track_thing = 0  #bottle
            pts = Point()
            pts2 = Point()
            untrack = 0
            self.emergency_stop = 0
            # flag = 0
            # pub_flag.publish(flag)

            while (1):
                ret, frames, depth = vid.read()
                frame = frames[0]
                depth_frame = frames[1]

                if ret == True:
                    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
                    dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180],
                                              1)

                    # apply meanshift to get the new location
                    ok, track_window = tracker.update(frame)
                    x, y, w, h = track_window

                    ok, track_window2 = tracker2.update(frame)
                    x2, y2, w2, h2 = track_window2

                    # Draw it on image
                    img2 = cv2.rectangle(frame, (x, y), (x + w, y + h), 255, 2)
                    if not track_thing:
                        img2 = cv2.rectangle(img2, (x2, y2),
                                             (x2 + w2, y2 + h2), 255, 2)
                    else:
                        img2 = cv2.rectangle(img2, (x2, y2),
                                             (x2 + w2, y2 + h2), (0, 0, 255),
                                             2)
                    # cv2.imshow('Tracking',img2)
                    tracking_frame = bridge.cv2_to_imgmsg(img2, "bgr8")
                    pub_frame2.publish(tracking_frame)

                    # https://www.intelrealsense.com/wp-content/uploads/2020/06/Intel-RealSense-D400-Series-Datasheet-June-2020.pdf
                    total, cnt = 0, 0
                    for i in range(3):
                        for j in range(3):
                            dep = depth.get_distance(
                                np.maximum(0, np.minimum(i + x + w // 2, 639)),
                                np.maximum(0, np.minimum(j + y + h // 2, 479)))
                            if (dep) != 0:
                                total += dep
                                cnt += 1
                    if cnt != 0:
                        worldz = total / cnt
                        # 人にぶつからないように距離を確保するため
                        if (worldz < 0.6) or (worldz > 3.0):
                            worldz = 0
                    else:
                        worldz = 0

                    total2, cnt2 = 0, 0
                    for i in range(3):
                        for j in range(3):
                            dep2 = depth.get_distance(
                                np.maximum(0,
                                           np.minimum(i + x2 + w2 // 2, 639)),
                                np.maximum(0,
                                           np.minimum(j + y2 + h2 // 2, 479)))
                            if dep2 != 0:
                                total2 += dep2
                                cnt2 += 1
                    if cnt2 != 0:
                        worldz2 = total2 / cnt2
                        if (worldz2 < 0.6) or (worldz2 > 3.0):
                            worldz2 = 0
                    else:
                        worldz2 = 0

                    # print('worldz', worldz)
                    # print('worldz2', worldz2)
                    # if (worldz == 0) or (worldz2 == 0):
                    if worldz2 == 0:
                        # break
                        worldx, worldy = 0, 0
                        # worldx = 0
                        pts.x, pts.y, pts.z = 0.0, 0.0, 0.0
                        worldx2, worldy2 = 0, 0
                        pts2.x, pts2.y, pts2.z = 0.0, 0.0, 0.0
                    else:
                        # focus length = 1.93mm, distance between depth cameras = about 5cm, a pixel size = 3um
                        if (track_thing == 0) and (not worldz == 0):
                            #bottle Tracking
                            u_ud = (0.05 * 1.88 * 10**(-3)) / (3 * 10**(-6) *
                                                               worldz)
                            # print('u_ud', u_ud)
                            # print('x, y =', (x+w//2)-(img2.shape[1]//2), (img2.shape[0]//2)-(y+h//2))
                            # 深度カメラとカラーカメラの物理的な距離を考慮した項(-0.3*u_ud)
                            # これらの座標は物体を見たときの左の深度カメラを基準とする
                            worldx = 0.05 * (x + w // 2 -
                                             (img2.shape[1] // 2) -
                                             0.3 * u_ud) / u_ud
                            worldy = 0.05 * ((img2.shape[0] // 2) -
                                             (y + h)) / u_ud
                            print('x,y,z = ', worldx, worldy, worldz - 0.6)
                            pts.y, pts.z, pts.x = -1.0 * float(worldx), float(
                                worldy), float(worldz) - 0.6
                            # pts.y, pts.z, pts.x = float(0.0), float(worldy), float(1.0)

                        else:
                            #human Tracking
                            # if worldz==0:
                            #     worldx, worldy = 0, 0
                            #     pts.x, pts.y, pts.z = 0.0, 0.0, 0.0
                            u_ud = (0.05 * 1.88 * 10**(-3)) / (3 * 10**(-6) *
                                                               worldz2)
                            worldx2 = 0.05 * (x2 + w2 // 2 -
                                              (img2.shape[1] // 2) -
                                              0.3 * u_ud) / u_ud
                            worldy2 = 0.05 * ((img2.shape[0] // 2) -
                                              (y2 + h2)) / u_ud
                            print('x2,y2,z2 = ', worldx2, worldy2,
                                  worldz2 - 0.6)
                            pts2.y, pts2.z, pts2.x = -1.0 * float(
                                worldx2), float(worldy2), float(worldz2) - 0.6
                            # pts.y, pts.z, pts.x = float(0.0), float(worldy), float(1.0)
                            if worldz == 0:
                                worldx, worldy = 0, 0
                                pts.x, pts.y, pts.z = 0.0, 0.0, 0.0

                    print("track_thing = ", track_thing)

                    frame_b, frame_g, frame_r = frame[:, :,
                                                      0], frame[:, :,
                                                                1], frame[:, :,
                                                                          2]
                    hist_b2 = cv2.calcHist([frame_b[y:y + h, x:x + w]], [0],
                                           None, [256], [0, 256])
                    hist_g2 = cv2.calcHist([frame_g[y:y + h, x:x + w]], [0],
                                           None, [256], [0, 256])
                    hist_r2 = cv2.calcHist([frame_r[y:y + h, x:x + w]], [0],
                                           None, [256], [0, 256])
                    cv2.normalize(hist_b2, hist_b2, 0, 255, cv2.NORM_MINMAX)
                    cv2.normalize(hist_g2, hist_g2, 0, 255, cv2.NORM_MINMAX)
                    cv2.normalize(hist_r2, hist_r2, 0, 255, cv2.NORM_MINMAX)
                    hist_bp2 = cv2.calcHist([frame_b[y2:y2 + h2, x2:x2 + w2]],
                                            [0], None, [256], [0, 256])
                    hist_gp2 = cv2.calcHist([frame_g[y2:y2 + h2, x2:x2 + w2]],
                                            [0], None, [256], [0, 256])
                    hist_rp2 = cv2.calcHist([frame_r[y2:y2 + h2, x2:x2 + w2]],
                                            [0], None, [256], [0, 256])
                    cv2.normalize(hist_bp2, hist_bp2, 0, 255, cv2.NORM_MINMAX)
                    cv2.normalize(hist_gp2, hist_gp2, 0, 255, cv2.NORM_MINMAX)
                    cv2.normalize(hist_rp2, hist_rp2, 0, 255, cv2.NORM_MINMAX)
                    comp_b = cv2.compareHist(hist_b, hist_b2,
                                             cv2.HISTCMP_CORREL)
                    comp_g = cv2.compareHist(hist_g, hist_g2,
                                             cv2.HISTCMP_CORREL)
                    comp_r = cv2.compareHist(hist_r, hist_r2,
                                             cv2.HISTCMP_CORREL)
                    comp_bp = cv2.compareHist(hist_bp, hist_bp2,
                                              cv2.HISTCMP_CORREL)
                    comp_gp = cv2.compareHist(hist_gp, hist_gp2,
                                              cv2.HISTCMP_CORREL)
                    comp_rp = cv2.compareHist(hist_rp, hist_rp2,
                                              cv2.HISTCMP_CORREL)
                    # print('compareHist(b)', comp_b)
                    # print('compareHist(g)', comp_g)
                    # print('compareHist(r)', comp_r)
                    # print('compareHist(bp)', comp_bp)
                    # print('compareHist(gp)', comp_gp)
                    # print('compareHist(rp)', comp_rp)
                    # print("garbage_in_can", garbage_in_can)
                    # 追跡を止める条件は,bottle追跡中にヒストグラムが大きく変化するか枠が無くなるまたはpersonを見失う,もしくはperson追跡中にヒストグラムが大きく変化するか枠が無くなるまたはゴミがゴミ箱に入れられた,
                    if ((track_thing == 0 and
                         ((comp_b <= 0.1) or (comp_g <= 0.1) or
                          (comp_r <= 0.1) or track_window == (0, 0, 0, 0)))
                            or (track_window2 == (0, 0, 0, 0))
                            or (track_thing == 1 and
                                ((comp_bp <= 0.) or (comp_gp <= 0.) or
                                 (comp_rp <= 0.)))):
                        untrack += 1
                        print("untrack = ", untrack)
                        if untrack >= 30:
                            print("追跡が外れた!\n")
                            break
                    elif (track_thing == 0 and (x + w > 640 or x < 0) and
                          (y + h > 480
                           or y < 0)) or (track_thing == 1 and
                                          (x2 + w2 > 640 or x2 < 0) and
                                          (y2 + h2 > 480 or y2 < 0)):
                        untrack += 1
                        print("untrack = ", untrack)
                        if untrack >= 50:
                            print("枠が画面外で固まった")
                            break
                    # elif (track_thing==1 and self.garbage_in_can==1):
                    elif self.garbage_in_can == 1:
                        print("ゴミを捨てたため追跡を終えます")
                        flag = 0
                        break
                    else:
                        untrack = 0
                    # print('garbage_in_can', self.garbage_in_can)

                    # ポイ捨ての基準はbottleを追跡していて,地面から10cmのところまで落ちたか,bottleを見失ったかつ見失う前のフレームでの高さがカメラの10cmより下
                    # print('track_window = ', track_window)
                    if (((worldy <= -0.01) or
                         ((track_window == (0, 0, 0, 0) or untrack >= 1) and
                          (worldy < 0.5))) and (not track_thing)):
                        print("ポイ捨てした!\n")
                        track_thing = 1  #human

                    # if track_thing==0:
                    #     tracking_point = pts
                    #     if not (pts.x==0.0 and pts.y==0.0 and pts.z==0.0):
                    #         pub.publish(tracking_point)
                    #     flag = 0 #bottle
                    # else:
                    #     tracking_point = pts2
                    #     if not (pts2.x==0.0 and pts2.y==0.0 and pts2.z==0.0):
                    #         pub.publish(tracking_point)
                    #     flag = 1 #person
                    if track_thing == 1:
                        tracking_point = pts2
                        if not (pts2.x == 0.0 and pts2.y == 0.0
                                and pts2.z == 0.0):
                            pub.publish(tracking_point)
                        flag = 1
                    # else:
                    #     flag = 0

                    pub_flag.publish(flag)

                    k = cv2.waitKey(1) & 0xff
                    # print("emergency_stop", self.emergency_stop)
                    # if (k == 27) or self.emergency_stop==1: # dev
                    if self.emergency_stop == 1:  # ops
                        print('emergency_stop == 1')
                        break
                else:
                    break
                # pub_track.publish(1)
            # pub_flag.publish(flag)

        yolo.close_session()
Example #46
0
def store_images(g_id):
    total_pics = 1200
    if g_id == str(0):
        create_empty_images("0", total_pics)
        return
    hist = get_hand_hist()
    cam = cv2.VideoCapture(1)
    x, y, w, h = 300, 100, 300, 300

    create_folder("gestures/" + str(g_id))
    pic_no = 0
    flag_start_capturing = False
    frames = 0

    while True:
        img = cam.read()[1]
        img = cv2.flip(img, 1)
        imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
        dst = cv2.calcBackProject([imgHSV], [0, 1], hist, [0, 180, 0, 256], 1)
        disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10))
        cv2.filter2D(dst, -1, disc, dst)
        blur = cv2.GaussianBlur(dst, (11, 11), 0)
        blur = cv2.medianBlur(blur, 15)
        thresh = cv2.threshold(blur, 0, 255,
                               cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
        thresh = cv2.merge((thresh, thresh, thresh))
        thresh = cv2.cvtColor(thresh, cv2.COLOR_BGR2GRAY)
        thresh = thresh[y:y + h, x:x + w]
        contours = cv2.findContours(thresh.copy(), cv2.RETR_TREE,
                                    cv2.CHAIN_APPROX_NONE)[1]

        if len(contours) > 0:
            contour = max(contours, key=cv2.contourArea)
            if cv2.contourArea(contour) > 10000 and frames > 50:
                x1, y1, w1, h1 = cv2.boundingRect(contour)
                pic_no += 1
                save_img = thresh[y1:y1 + h1, x1:x1 + w1]
                if w1 > h1:
                    save_img = cv2.copyMakeBorder(save_img, int((w1 - h1) / 2),
                                                  int((w1 - h1) / 2), 0, 0,
                                                  cv2.BORDER_CONSTANT,
                                                  (0, 0, 0))
                elif h1 > w1:
                    save_img = cv2.copyMakeBorder(save_img, 0, 0,
                                                  int((h1 - w1) / 2),
                                                  int((h1 - w1) / 2),
                                                  cv2.BORDER_CONSTANT,
                                                  (0, 0, 0))
                save_img = cv2.resize(save_img, (image_x, image_y))
                cv2.putText(img, "Capturing...", (30, 60),
                            cv2.FONT_HERSHEY_TRIPLEX, 2, (127, 255, 255))
                cv2.imwrite(
                    "gestures/" + str(g_id) + "/" + str(pic_no) + ".jpg",
                    save_img)

        cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
        cv2.putText(img, str(pic_no), (30, 400), cv2.FONT_HERSHEY_TRIPLEX, 1.5,
                    (127, 127, 255))
        cv2.imshow("Capturing gesture", img)
        cv2.imshow("thresh", thresh)
        keypress = cv2.waitKey(1)
        if keypress == ord('c'):
            if flag_start_capturing == False:
                flag_start_capturing = True
            else:
                flag_start_capturing = False
                frames = 0
        if flag_start_capturing == True:
            frames += 1
        if pic_no == total_pics:
            break
Example #47
0
            hist = cv.calcHist([hsv_roi], [0], mask_roi, [16], [0, 180])

            # Normilize from 0 to 256
            cv.normalize(hist, hist, 0, 255, cv.NORM_MINMAX)
            # Transpose of histogram matrix
            hist = hist.reshape(-1)
            # Calling function to display the histogram
            show_hist(hist)

            # Initializing the area of tracking
            # Calculating from diag points, width and height
            track_window = (x0, y0, x1 - x0, y1 - y0)

            # Calculating the back proj of a hist (images, chanels, hist, ranges, scale[,dist})
            # obtain probability of each pixel of that region belonging to a face or not (depending on color)
            prob = cv.calcBackProject([hsv], [0], hist, [0, 180], 1)

            #prob = prob & mask
            prob &= mask

            term_crit = (cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 1) #termination criteria for camshift

            # Running camshift (first time)
            track_box, track_window = cv.CamShift(prob, track_window, term_crit)

             # Getting the coordinates of the face region and a little enalrging the box
            ((x3, y3), (w, z), k) = track_box
            x0 = int(x3 - w / 2)
            x1 = int(x3 + w / 2)
            y0 = int(y3 - z / 2)
            y1 = int(y3 + z / 2)
Example #48
0
def aslrun(frame2):
    letter_lookup = {
        0: 'A',
        1: 'B',
        2: 'C',
        3: 'D',
        4: 'E',
        5: 'F',
        6: 'G',
        7: 'H',
        8: 'I',
        9: 'J',
        10: 'K',
        11: 'L',
        12: 'M',
        13: 'N',
        14: 'O',
        15: 'P',
        16: 'Q',
        17: 'R',
        18: 'S',
        19: 'T',
        20: 'U',
        21: 'V',
        22: 'W',
        23: 'X',
        24: 'Y',
        25: 'Z',
        26: ' ',
        27: '&',
        28: '#'
    }
    number_lookup = {
        0: '1',
        1: '2',
        2: '3',
        3: '4',
        4: '5',
        5: '6',
        6: '7',
        7: '8',
        8: '9',
        9: '0',
        10: '#'
    }

    cont = frame2
    hist = hand_hist()

    imghsv = cv2.cvtColor(frame2, cv2.COLOR_BGR2HSV)
    dst = cv2.calcBackProject([imghsv], [0, 1], hist, [0, 180, 0, 256], 1)
    disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10))
    cv2.filter2D(dst, -1, disc, dst)
    blur = cv2.GaussianBlur(dst, (5, 5), 0)
    blur = cv2.medianBlur(blur, 7)
    kernel1 = np.ones((5, 5), np.uint8)
    blur = cv2.morphologyEx(blur, cv2.MORPH_DILATE, kernel1)

    cv2.imshow("Output", dst)
    cv2.imshow("Output1", blur)

    _, contours, hierarchy = cv2.findContours(blur.copy(), cv2.RETR_EXTERNAL,
                                              cv2.CHAIN_APPROX_SIMPLE)

    back_img = np.ones((cont.shape[0], cont.shape[1]), dtype="uint8")
    back_img = back_img * 255

    if len(contours) == 0:
        print("Not Detected")
        pred_list = []
    else:

        hand_segment = max(contours, key=cv2.contourArea)
        cv2.drawContours(back_img, [hand_segment], -1, (0, 0, 0), -1)
        cont = cv2.cvtColor(cont, cv2.COLOR_BGR2GRAY)
        cv2.bitwise_or(cont, back_img, dst=back_img)
        cv2.imshow("bi", back_img)

        ### Code when lighting is great
        frame2 = back_img
        kernel2 = np.ones(shape=(3, 3), dtype=np.float32) / 4
        frame2 = cv2.filter2D(frame2, -1, kernel2)
        cv2.imshow('filter', frame2)
        # frame2=cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
        kernel3 = np.ones((3, 3), np.uint8)
        gradient = cv2.morphologyEx(frame2, cv2.MORPH_GRADIENT, kernel3)
        cv2.imshow('image1', gradient)

        # predict character using the classifier model
        gradient2 = cv2.resize(gradient, (128, 128))
        gradient2 = gradient2.reshape(1, 128, 128, 1)

        if mode == 0:
            out = classifier1.predict_classes(gradient2)
            pred_var = letter_lookup[int(out)]

        elif mode == 1:
            out = classifier2.predict_classes(gradient2)
            pred_var = number_lookup[int(out)]

        return pred_var
Example #49
0
    # Process Detected People
    if len(detectedPeople) != 0:
        for people in detectedPeople:

            # Setup Meanshift/Camshift for Tracking
            track_window = (people.x, people.y, people.w, people.h)
            hsv_roi = pOpening[int(people.y):int(people.y + people.h),
                               int(people.x):int(people.x + people.w)]
            mask = cv2.inRange(hsv_roi, np.array(128), np.array(256))
            roi_hist = cv2.calcHist([hsv_roi], [0], mask, [100], [0, 256])
            cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)
            term_criteria = (
                cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 1, 1
            )  # Setup the termination criteria, either 10 iteration or move by atleast 1 pt
            dst = cv2.calcBackProject([opening], [0], roi_hist, [0, 256], 1)
            ret, track_window = cv2.CamShift(dst, track_window, term_criteria)

            # Process POST Tracking
            pts = cv2.boxPoints(ret)
            pts = np.int0(pts)
            img2 = cv2.polylines(frameForView, [pts], True, people.color, 2)
            pos = sum(pts) / len(pts)
            isFound = False
            for dC in detectedContours:
                if dC.x - toleranceRange < pos[0] < dC.x + dC.w + toleranceRange \
                        and dC.y - toleranceRange < pos[1] < dC.y + dC.h + toleranceRange:
                    people.set("x", dC.x)
                    people.set("y", dC.y)
                    people.set("w", dC.w)
                    people.set("h", dC.h)
Example #50
0
        def start_tracking(self):
            #Iterate until the user presses the esc key
            while True:

                #Capture the frame from the webcam
                _, self.frame = self.cap.read()

                #resize the input frame
                self.frame = cv2.resize(self.frame,
                                        None,
                                        fx=self.scaling_factor,
                                        fy=self.scaling_factor,
                                        interpolation=cv2.INTER_AREA)

                #create a copy of the frame
                vis = self.frame.copy()

                #convert the frame to HSV colorspace
                hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV)

                #create the mask based on predefined
                mask = cv2.inRange(hsv, np.array(0., 60., 32.)), np.array(
                    (180., 255., 255.))

                #check if the user has select the region
                if self.selection:
                    x0, y0, x1, y1 = self.selection
                    self.track_window = (x0, y0, x1 - x0, y1 - y0)

                    hsv_roi = hsv[y0:y1, x0:x1]
                    mask_roi = mask[y0:y1, x0:x1]

                    hist = cv2.calcHist([hsv_roi], [0], mask_roi, [16],
                                        [0, 180])

                    cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)
                    self.hist = hist.reshape(-1)

                    vis_roi = vis[y0:y1, x0:x1]

                    cv2.bitwise_not(vis_roi, vis_roi)
                    vis[mask == 0] = 0

                if self.tracking_state == 1:
                    self.selection = None

                    hsv_backproj = cv2.calcBackProject([hsv], [0], self.hist,
                                                       [0, 180], 1)

                    hsv_backproj &= mask

                    term_crit = (cv2.TERM_CRITERIA_EPS
                                 | cv2.TERM_CRITERIA_COUNT, 10, 1)

                    track_box, self.track_window = cv2.CamShift(
                        hsv_backproj, self.track_window, term_crit)

                    cv2.ellipse(vis, track_box, (0, 255, 0), 2)

                cv2.imshow('Object Tracker', vis)

                c = cv2.waitKey(5)
                if c == 27:
                    break
Example #51
0
def prediction():
    def get_hand_hist():
        with open("hist", "rb") as f:
            hist = pickle.load(f)
        return hist

    image_x, image_y = (50, 50)

    def keras_process_image(img):  # test할 이미지를 배열 값으로 반환
        img = cv2.resize(img, (image_x, image_y))
        img = np.array(img, dtype=np.float32)
        img = np.reshape(img, (1, image_x, image_y, 1))
        return img

    def keras_predict(model, image):  # 배열값을 가지고 이미지 예측
        processed = keras_process_image(image)
        with graph.as_default():
            pred_probab = model.predict(processed)[0]
        pred_class = list(pred_probab).index(max(pred_probab))
        return max(pred_probab), pred_class

    def get_pred_text_from_db(pred_class):  # 데이터 베이스에서 이름 찾기
        conn = sqlite3.connect("gesture_db.db")
        cmd = "SELECT g_name FROM gesture WHERE g_id=" + str(pred_class)
        cursor = conn.execute(cmd)
        for row in cursor:
            return row[0]

    def get_pred_from_contour(contour, thresh):  # 이미지 판정
        x1, y1, w1, h1 = cv2.boundingRect(contour)
        save_img = thresh[y1:y1 + h1, x1:x1 + w1]
        name = ""
        if w1 > h1:
            save_img = cv2.copyMakeBorder(save_img, int((w1 - h1) / 2),
                                          int((w1 - h1) / 2), 0, 0,
                                          cv2.BORDER_CONSTANT, (0, 0, 0))
        elif h1 > w1:
            save_img = cv2.copyMakeBorder(save_img, 0, 0, int((h1 - w1) / 2),
                                          int((h1 - w1) / 2),
                                          cv2.BORDER_CONSTANT, (0, 0, 0))
        pred_probab, pred_class = keras_predict(model, save_img)
        if pred_probab * 100 > 70:
            name = get_pred_text_from_db(pred_class)
        return name

    keras_predict(model, np.zeros((50, 50), dtype=np.uint8))

    hist = get_hand_hist()  # 저장된 손의 히스토그램
    x, y, w, h = 300, 100, 300, 300

    global cam
    cam = cv2.VideoCapture(1)
    if cam.read()[0] == False:
        cam = cv2.VideoCapture(0)
    name = ""
    line = ""

    path_name = "static/output.txt"

    T = open(path_name, 'wt', encoding='utf-8')
    T.write("")
    T.close()
    end = 0
    while True:
        img = cam.read()[1]  # 이미지 컨투어링 시키는 함수
        img = cv2.resize(img, (640, 480))
        img = cv2.flip(img, 1)
        imgHSV = cv2.cvtColor(
            img, cv2.COLOR_BGR2HSV)  # bgr색공간의 이미지를 hsv 색공간 이미지로 변환
        dst = cv2.calcBackProject([imgHSV], [0, 1], hist, [0, 180, 0, 256],
                                  1)  # 배경 투영, 원하는 객체 영역 검출
        disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,
                                         (10, 10))  # 윤곽선 보정에 필요함
        cv2.filter2D(dst, -1, disc, dst)  # 윤곽선 부드러워지도록 필터 끼워줌
        blur = cv2.GaussianBlur(dst, (11, 11), 0)
        blur = cv2.medianBlur(blur, 15)
        thresh = cv2.threshold(blur, 0, 255,
                               cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
        # 이미지 문턱값 지정해주고 이미지 픽셀값이 문턱값보다 크면 255 값을 갖고 문턱보다 작으면 0을 갖도록(픽셀분류)
        thresh = cv2.merge(
            (thresh, thresh, thresh))  # 1채널의 바이너리 이미지를 3채널 이미지로 변환
        thresh = cv2.cvtColor(thresh, cv2.COLOR_BGR2GRAY)  # 흑백으로 색변환
        thresh = thresh[y:y + h, x:x + w]
        contours = cv2.findContours(thresh.copy(), cv2.RETR_TREE,
                                    cv2.CHAIN_APPROX_NONE)[1]  # 만들어진 윤곽선 값
        if len(contours) > 0:  # 컨투어링 값이 생기면
            end = 0
            contour = max(contours, key=cv2.contourArea)  # 최대값 뽑아서
            keypress = cv2.waitKey(1)
            if cv2.contourArea(contour) > 10000 and keypress == ord(
                    'c'):  # C 눌렀을때
                name = get_pred_from_contour(
                    contour, thresh)  # 데이터베이스에서 컨투어링, 임계값에 일치하는 이름 들고옴
                print("name : " + name)
                R = open(path_name, 'rt', encoding='utf-8')
                line = ""
                while True:
                    line = line + R.readline()
                    if not R.readline(): break
                R.close()
                T = open(path_name, 'wt', encoding='utf-8')
                name = line + name
                T.write(name)
                T.close()
                sentence = join_jamos(name)
                print(sentence)
            elif cv2.contourArea(contour) < 1000 and keypress == ord('c'):
                name = " "
                #print("not found")
            elif keypress == ord('f'):
                end = 1
            elif keypress == ord('e'):
                R = open(path_name, 'rt', encoding='utf-8')
                line = ""
                while True:
                    line = line + R.readline()
                    if not R.readline(): break
                R.close()
                newline = ""
                for i in range(0, len(line) - 1):
                    newline = newline + line[i]
                T = open(path_name, 'wt', encoding='utf-8')
                T.write(newline)
                line = newline
                print("", line)
                T.close()

        R = open(path_name, 'r', encoding='utf-8')
        new_line = ""
        while True:
            new_line = new_line + R.readline()
            if not R.readline(): break
        T = open(path_name, 'wt', encoding='utf-8')
        sentence = join_jamos(new_line)
        T.write(sentence)

        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
        # cv2.putText(img, str(line), (30, 400), cv2.FONT_HERSHEY_DUPLEX, 1.5, (255, 255, 255))
        cv2.imshow("Capturing gesture", img)
        cv2.imshow("output", thresh)
        if end == 1:
            cam.release()
            cv2.destroyAllWindows()
            break
Example #52
0
    def run(self):
        while True:
            _ret, self.frame = self.cam.read()
            vis = self.frame.copy()
            hsv = cv.cvtColor(
                src=self.frame, code=cv.COLOR_BGR2HSV
            )  # convert to HSV to reduce the influence of lightness
            mask = cv.inRange(src=hsv,
                              lowerb=np.array((0., 60., 32.)),
                              upperb=np.array((180., 255., 255.)))

            # if self.selection:
            #     x0, y0, x1, y1 = self.selection
            #     hsv_roi = hsv[y0:y1, x0:x1]
            #     mask_roi = mask[y0:y1, x0:x1]
            #     hist = cv.calcHist([hsv_roi], [0], mask_roi, [16], [0, 180] )
            #     cv.normalize(hist, hist, 0, 255, cv.NORM_MINMAX)
            #     self.hist = hist.reshape(-1)
            #     self.show_hist()
            #
            #     vis_roi = vis[y0:y1, x0:x1]
            #     cv.bitwise_not(vis_roi, vis_roi)
            #     vis[mask == 0] = 0
            if self.selections:
                for idx, selection in enumerate(self.selections):
                    if selection:
                        x0, y0, x1, y1 = selection
                        hsv_roi = hsv[y0:y1, x0:x1]
                        mask_roi = mask[y0:y1, x0:x1]
                        hist = cv.calcHist(images=[hsv_roi],
                                           channels=[0],
                                           mask=mask_roi,
                                           histSize=[16],
                                           ranges=[0, 180])
                        cv.normalize(src=hist,
                                     dst=hist,
                                     alpha=0,
                                     beta=255,
                                     norm_type=cv.NORM_MINMAX)
                        # self.hist = hist.reshape(-1)
                        try:
                            self.hist_list[idx] = hist.reshape(
                                -1)  # update hist
                        except:
                            self.hist_list.append(
                                hist.reshape(-1))  # add hist if select new
                        self.show_hist(idx)

                        vis_roi = vis[y0:y1, x0:x1]
                        cv.bitwise_not(vis_roi, vis_roi)
                        vis[mask == 0] = 0
                    self.show_hist(idx)

            # if self.track_window and self.track_window[2] > 0 and self.track_window[3] > 0:
            #     self.selection = None
            #     prob = cv.calcBackProject([hsv], [0], self.hist, [0, 180], 1)
            #     prob &= mask
            #     term_crit = (cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 1)
            #     track_box, self.track_window = cv.CamShift(prob, self.track_window, term_crit)
            #
            #     if self.show_backproj:
            #         vis[:] = prob[...,np.newaxis]
            #     try:
            #         cv.ellipse(vis, track_box, (0, 0, 255), 2)
            #     except:
            #         print(track_box)
            if self.track_windows:
                for idx, track_window in enumerate(self.track_windows):
                    if track_window and track_window[2] > 0 and track_window[
                            3] > 0:
                        self.selections[
                            idx] = None  # FIXME: comment this line for refresh hist, however, that will lead to bad display
                        prob = cv.calcBackProject(images=[hsv],
                                                  channels=[0],
                                                  hist=self.hist_list[idx],
                                                  ranges=[0, 180],
                                                  scale=1)
                        prob &= mask
                        term_crit = (cv.TERM_CRITERIA_EPS
                                     | cv.TERM_CRITERIA_COUNT, 10, 1)
                        track_box, self.track_windows[idx] = cv.CamShift(
                            probImage=prob,
                            window=track_window,
                            criteria=term_crit)

                        if self.show_backproj:
                            vis[:] = prob[..., np.newaxis]
                        try:
                            cv.ellipse(vis, track_box,
                                       self.track_box_colors[idx], 2)
                        except:
                            print(track_box)

            cv.imshow('camshift', vis)

            ch = cv.waitKey(5)
            if ch == 27:
                break
            if ch == ord('b'):
                self.show_backproj = not self.show_backproj
        cv.destroyAllWindows()
Example #53
0
	cols=frame.shape[1]
	cv2.rectangle(frame, (int(rows/3),int(cols/2.5)), (int(rows/3+SQ_SIZE),int(cols/2.5)+SQ_SIZE), (0,255,255))
	cv2.rectangle(frame, (int(rows/4.2),int(cols/2.9)), (int(rows/4.2+SQ_SIZE),int(cols/2.9)+SQ_SIZE), (0,255,255))
	cv2.rectangle(frame, (int(rows/2.7),int(cols/3.6)), (int(rows/2.7+SQ_SIZE),int(cols/3.6)+SQ_SIZE), (0,255,255))
	cv2.rectangle(frame, (int(rows/4),int(cols/2.3)), (int(rows/4+SQ_SIZE),int(cols/2.3)+SQ_SIZE), (0,255,255))
	cv2.rectangle(frame, (int(rows/2),int(cols/2.5)), (int(rows/2+SQ_SIZE),int(cols/2.5)+SQ_SIZE), (0,255,255))
	cv2.rectangle(frame, (int(rows/2.7),int(cols/8)), (int(rows/2.7+SQ_SIZE),int(cols/8)+SQ_SIZE), (0,255,255))

	roi_1=orig[int(cols/2.5):int(cols/2.5)+SQ_SIZE, int(rows/3):int(rows/3+SQ_SIZE)]
	rh1 = cv2.calcHist([roi_1],[0, 1], None, [256, 256], [0, 256, 0, 256] )
	if(reset):
		mean_1=cv2.mean(roi_1)
	# TR_MIN = np.array([0, max(mean_1[1]-sat_sens,0), mean_1[2]-val_sens],np.uint8)
	# TR_MAX = np.array([mean_1[0]+hue_sens, mean_1[1]+sat_sens, mean_1[2]+val_sens],np.uint8)
	# tr_1=cv2.inRange(orig, TR_MIN, TR_MAX)
	tr_1=cv2.calcBackProject([frame],[0,1],rh1,[0,256,0,256],1)
	
	roi_2=orig[int(cols/2.9):int(cols/2.9)+SQ_SIZE, int(rows/4.2):int(rows/4.2+SQ_SIZE)]
	rh2 = cv2.calcHist([roi_2],[0, 1], None, [256, 256], [0, 256, 0, 256] )
	if(reset):
		mean_2=cv2.mean(roi_2)
	# TR_MIN = np.array([0, max(mean_2[1]-sat_sens,0), mean_2[2]-val_sens],np.uint8)
	# TR_MAX = np.array([mean_2[0]+hue_sens, mean_2[1]+sat_sens, mean_2[2]+val_sens],np.uint8)
	# tr_2=cv2.inRange(orig, TR_MIN, TR_MAX)
	tr_2=cv2.calcBackProject([frame],[0,1],rh2,[0,256,0,256],1)



	roi_3=orig[int(cols/3.6):int(cols/3.6)+SQ_SIZE, int(rows/2.7):int(rows/2.7+SQ_SIZE)]
	rh3 = cv2.calcHist([roi_3],[0, 1], None, [256, 256], [0, 256, 0, 256] )
	if(reset):
Example #54
0
def get_hand_hist():
    def build_squares(img):
        x, y, w, h = 420, 140, 10, 10
        d = 10
        imgCrop = None
        crop = None
        for i in range(10):
            for j in range(5):
                if np.any(imgCrop == None):
                    imgCrop = img[y:y + h, x:x + w]
                else:
                    imgCrop = np.hstack((imgCrop, img[y:y + h, x:x + w]))
                # print(imgCrop.shape)
                cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 1)
                x += w + d
            if np.any(crop == None):
                crop = imgCrop
            else:
                crop = np.vstack((crop, imgCrop))
            imgCrop = None
            x = 420
            y += h + d
        return crop

    cam = cv2.VideoCapture(1)
    if cam.read()[0] == False:
        cam = cv2.VideoCapture(0)
    x, y, w, h = 300, 100, 300, 300
    flagPressedC, flagPressedS = False, False
    imgCrop = None
    while True:
        img = cam.read()[1]
        img = cv2.flip(img, 1)
        img = cv2.resize(img, (640, 480))
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

        keypress = cv2.waitKey(1)
        if keypress == ord('c'):
            hsvCrop = cv2.cvtColor(imgCrop, cv2.COLOR_BGR2HSV)
            flagPressedC = True
            hist = cv2.calcHist([hsvCrop], [0, 1], None, [180, 256],
                                [0, 180, 0, 256])
            cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)
        elif keypress == ord('s'):
            flagPressedS = True
            break
        if flagPressedC:
            dst = cv2.calcBackProject([hsv], [0, 1], hist, [0, 180, 0, 256], 1)
            dst1 = dst.copy()
            disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10))
            cv2.filter2D(dst, -1, disc, dst)
            blur = cv2.GaussianBlur(dst, (11, 11), 0)
            blur = cv2.medianBlur(blur, 15)
            ret, thresh = cv2.threshold(blur, 0, 255,
                                        cv2.THRESH_BINARY + cv2.THRESH_OTSU)
            thresh = cv2.merge((thresh, thresh, thresh))
            # cv2.imshow("res", res)
            cv2.imshow("output", thresh)
        if not flagPressedS:
            imgCrop = build_squares(img)
        # cv2.rectangle(img, (x,y), (x+w, y+h), (0,255,0), 2)
        cv2.imshow("Set hand color", img)
    cam.release()
    cv2.destroyAllWindows()
    with open("hist", "wb") as f:
        pickle.dump(hist, f)

    return render_template("main.html")
def run_main():
    #To save the video later
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter('output1.avi ', fourcc, 20.0, (640, 480))

    cap = cv2.VideoCapture('video/thermal_stereo_mv.mp4')
    #cap = cv2.VideoCapture('video/WhatsApp.mp4')

    # Read the first frame of the video
    ret, frame = cap.read()
    height, width, channels = frame.shape
    print(height, width, channels)
    frame = frame[0:480, 0:1280]  #left
    #frame = frame[0:480,640:1280]  # right

    #feature detection
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    corners = cv2.goodFeaturesToTrack(gray, 25, 0.01, 10)
    corners = np.int0(corners)

    # Set the ROI (Region of Interest). Actually, this is a
    # rectangle of the building that we're tracking

    x, y = corners[22].ravel()
    c, r, w, h = x, y, 50, 50
    track_window = (c, r, w, h)

    # Create mask and normalized histogram
    roi = frame[r:r + h, c:c + w]
    #hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
    hsv_roi = roi

    lower_grey = np.array([0, 255, 255])
    upper_grey = np.array([10, 255, 255])
    #mask = cv2.inRange(hsv_roi, np.array((0., 30., 32.)), np.array((180., 255., 255.)))
    mask = cv2.inRange(hsv_roi, lower_grey, upper_grey)
    #roi_hist = cv2.calcHist([hsv_roi], [0], mask, [180], [0, 180])
    roi_hist = cv2.calcHist([hsv_roi], [0], None, [256], [0, 256])
    cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)
    term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 80, 1)

    #grey = np.uint8([[[0, 0, 255]]])
    #hsv_grey = cv2.cvtColor(grey, cv2.COLOR_BGR2HSV)
    #print (hsv_grey)

    while True:
        ret, frame = cap.read()

        #hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        #dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)
        dst = cv2.calcBackProject([frame], [0], roi_hist, [0, 256], 1)

        ret, track_window = cv2.meanShift(dst, track_window, term_crit)

        x, y, w, h = track_window
        cv2.rectangle(frame, (x, y), (x + w, y + h), 255, 2)
        cv2.putText(frame, 'Tracked', (x - 25, y - 10),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2,
                    cv2.CV_8UC1)  #CV_AA)
        out.write(frame)
        cv2.imshow('Tracking', frame)

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

    cap.release()
    out.release()
    cv2.destroyAllWindows()
Example #56
0
import numpy as np

cap = cv.VideoCapture()
ret, frame = cap.read()

face_cascade = cv.CascadeClassifier('')
face_rects = face_cascade.detectMultiScale(frame)

x, y, w, h = tuple(face_rects[0])
track_window = (x, y, w, h)
roi = frame[x:y + h, x:x + w]

hsv_roi = cv.cvtColor(roi, cv.COLOR_BGR2HSV)

roi_hist = cv.calcHist([hsv_roi, [0], None, [180], [0, 180]])
cv.normalize(roi_hist, roi_hist, 0, 255, cv.NORM_MINIMAX)
term_crit = (cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 1)
while True:
    ret, frame = cap.read()
    if ret == True:
        hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
        dest_roi = cv.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)
        ret, track_window = cv.meanShift(dest_roi, track_window, term_crit)
        x, y, w, h = track_window
        img2 = cv.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 0), 3)
        cv.imshow('FaceTracker', img2)
        if cv.waitKey(50) & 0xFF == ord('q'):
            break
cap.release()
cv.destroyAllWindows()
Example #57
0
roi = frame[r:r + h, c:c + w]
hsv_roi = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

mask = cv2.inRange(hsv_roi, np.array((0., 60., 32.)),
                   np.array((180., 255., 255.)))
roi_hist = cv2.calcHist([hsv_roi], [0], mask, [180], [0, 180])
cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)

# Setup the termination criteria, either 10 iteration or move by atleast 1 pt
term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)

while (1):
    ret, frame = cap.read()
    if ret == True:
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)

        # apply meanshift to get the new location
        ret, track_window = cv2.CamShift(dst, track_window, term_crit)
        # Draw it on image
        pts = cv2.boxPoints(ret)
        pts = np.int0(pts)
        img2 = cv2.polylines(frame, [pts], True, 255, 2)
        cv2.imshow('img2', img2)

        k = cv2.waitKey(60) & 0xff
        if k == 27:
            break
        else:
            cv2.imwrite(chr(k) + ".jpg", img2)
    else:
Example #58
0
def filterImageForHand(frame, histogram):
    backProject = cv2.calcBackProject([frame], [0,1], histogram, [0, 180, 0, 256], scale=1)
    cv2.imshow('BackProj', backProject)
Example #59
0
            top_left = (boxes[0][0], boxes[0][1])
            bottom_right = (current_mouse_position[0],
                            current_mouse_position[1])
            cv2.rectangle(frame, top_left, bottom_right, (0, 255, 0), 2)

        # if we have a selected region

        if (cropped):

            # convert incoming image to HSV

            img_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

            # back projection of histogram based on Hue and Saturation only

            img_bproject = cv2.calcBackProject([img_hsv], [0, 1], crop_hist,
                                               [0, 180, 0, 255], 1)
            cv2.imshow(window_name2, img_bproject)

            # apply camshift to predict new location (observation)
            # basic HSV histogram comparision with adaptive window size
            # see :
            # http://docs.opencv.org/3.1.0/db/df8/tutorial_py_meanshift.html
            ret, track_window = cv2.CamShift(img_bproject, track_window,
                                             term_crit)

            # draw observation on image - in BLUE
            x, y, w, h = track_window
            frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0),
                                  2)

            # extract centre of this observation as points
Example #60
0
    def _tracking(self, frame):
        vis = frame.copy()
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        mask = cv2.inRange(hsv, np.array((0., 60., 32.)),
                           np.array((180., 255., 255.)))

        cv2.polylines(vis, [np.array([[500, 1056], [600, 1200]], np.int32)],
                      False, (0, 255, 0))

        if self.selection:
            x0, y0, x1, y1 = self.selection
            hsv_roi = hsv[y0:y1, x0:x1]
            mask_roi = mask[y0:y1, x0:x1]
            self.hist = cv2.calcHist([hsv_roi], [0], mask_roi, [16], [0, 180])
            cv2.normalize(self.hist, self.hist, 0, 255, cv2.NORM_MINMAX)
            self.hist = self.hist.reshape(-1)

            vis_roi = vis[y0:y1, x0:x1]
            cv2.bitwise_not(vis_roi, vis_roi)
            # vis[mask == 0] = 0

        if self.trackWindow and self.trackWindow[2] > 0 and self.trackWindow[
                3] > 0:
            self.selection = None
            prob = cv2.calcBackProject([hsv], [0], self.hist, [0, 180], 1)
            prob &= mask
            term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10,
                         1)
            track_box, self.trackWindow = cv2.CamShift(prob, self.trackWindow,
                                                       term_crit)

            if self.showBackproj:
                vis[:] = prob[..., np.newaxis]
            try:
                cv2.ellipse(vis, track_box, (0, 0, 255), 2)
                point_track = track_box[0]

                if (point_track != (0, 0)) and isinstance(
                        point_track, tuple) and (point_track
                                                 not in self.trackPoints):
                    self.timestamps.append(
                        self.capture.get(0))  # get timestamp
                    self.trackPoints.append(point_track)

                    if self.isCalibrated:
                        point_move = (point_track[0] - self.trackPoints[0][0],
                                      point_track[1] - self.trackPoints[0][1])
                        point_move = tuple(map(self._pixel2unit, point_move))
                        self.movePoints.append(point_move)
                        self.labelPos.setText(
                            "<font color='red'>({:.1f}ms: {:.4f}cm, {:.4f}cm)</font>"
                            .format(self.timestamps[-1], point_move[0],
                                    point_move[1]))

                cv2.circle(vis, (int(point_track[0]), int(point_track[1])), 2,
                           (0, 255, 0), -1)
                cv2.polylines(vis, [
                    np.array([np.int32(list(tr)) for tr in self.trackPoints])
                ], False, (0, 255, 0))

                # display tracking point
                self.labelTrack.setText(
                    "<font color='red'>({:.1f}ms: {:.0f}, {:.0f})</font>".
                    format(self.timestamps[-1], point_track[0],
                           point_track[1]))

            except Exception as e:
                print("Exception while drawing")
                print(e)
                print(track_box)
                # raise e

        return vis