|
| 1 | +import cv2 |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +#WARNING! OpenCV 3.* required. |
| 5 | + |
| 6 | +# Parameters for lucas kanade optical flow |
| 7 | +lk_params = dict( winSize = (15,15), |
| 8 | + maxLevel = 2, |
| 9 | + criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03)) |
| 10 | + |
| 11 | + |
| 12 | +def getHarrisPoints(gray_img): |
| 13 | + harris = cv2.cornerHarris(gray_img, 2, 3, 0.05) |
| 14 | + # Threshold for an optimal value |
| 15 | + xarr,yarr = np.nonzero(harris > 0.03*harris.max()) |
| 16 | + harrisPointsArray = [np.array([[x,y]]) for x,y in zip(xarr,yarr)] |
| 17 | + return np.array(harrisPointsArray, dtype=np.float32) |
| 18 | + |
| 19 | + |
| 20 | +def getFASTPoints(gray_img): |
| 21 | + # Initiate FAST object with default values |
| 22 | + fast = cv2.FastFeatureDetector_create(threshold=5) |
| 23 | + # find keypoints |
| 24 | + kp_array = fast.detect(gray_img,None) |
| 25 | + # print kp_array[0].pt[1] |
| 26 | + fastPointsArray = [np.array([[kp.pt[0], kp.pt[1]]]) for kp in kp_array] |
| 27 | + return np.array(fastPointsArray, dtype=np.float32) |
| 28 | + |
| 29 | +def videoTracking(cap, slow_motion_delay, detector): |
| 30 | + # Create some random colors |
| 31 | + color = np.random.randint(0,255,(100,3)) |
| 32 | + # Take first frame and find corners in it |
| 33 | + ret, old_frame = cap.read() |
| 34 | + old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY) |
| 35 | + # Create a mask image for drawing purposes |
| 36 | + mask = np.zeros_like(old_frame) |
| 37 | + # Define the codec and create VideoWriter object |
| 38 | + # height, width, _ = old_frame.shape |
| 39 | + # fourcc = cv2.VideoWriter_fourcc(*'XVID') |
| 40 | + # out = cv2.VideoWriter('output_'+detector+'.avi',fourcc, slow_motion_delay, (width,height)) |
| 41 | + if detector == 'Harris': |
| 42 | + oldTrackPoints = getHarrisPoints(old_gray) |
| 43 | + elif detector == 'FAST': |
| 44 | + oldTrackPoints = getFASTPoints(old_gray) |
| 45 | + else: |
| 46 | + print('Not correct method') |
| 47 | + print "$$$ Checking if i can recognise your move, n1ggv" |
| 48 | + while not len(oldTrackPoints) and cap.isOpened(): |
| 49 | + ret, old_frame = cap.read() |
| 50 | + old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY) |
| 51 | + oldTrackPoints = getFASTPoints(old_gray) |
| 52 | + while cap.isOpened(): |
| 53 | + ret, frame = cap.read() |
| 54 | + track_img = None |
| 55 | + if ret: |
| 56 | + new_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) |
| 57 | + # calculate optical flow |
| 58 | + newTrackPoints, st, err = cv2.calcOpticalFlowPyrLK(old_gray, new_gray, oldTrackPoints, |
| 59 | + None, **lk_params) |
| 60 | + |
| 61 | + # Select good points |
| 62 | + if newTrackPoints is not None and len(newTrackPoints): |
| 63 | + good_new = newTrackPoints[st==1] |
| 64 | + good_old = oldTrackPoints[st==1] |
| 65 | + # draw the tracks |
| 66 | + for i,(new,old) in enumerate(zip(good_new,good_old)): |
| 67 | + a,b = new.ravel() |
| 68 | + c,d = old.ravel() |
| 69 | + mask = cv2.line(mask, (a,b),(c,d),color[i%len(color)].tolist(), 2) #(255,0,0), 2) |
| 70 | + frame = cv2.circle(frame,(a,b),2,color[i%len(color)].tolist(),-1)#(0,255,0),-1) |
| 71 | + track_img = cv2.add(frame, mask) |
| 72 | + #write tracked frame |
| 73 | + # out.write(track_img) |
| 74 | + # cv2.imshow('videoTracking '+detector, track_img) |
| 75 | + # if cv2.waitKey(slow_motion_delay) & 0xFF == ord('q'): |
| 76 | + # break |
| 77 | + |
| 78 | + # Now update the previous frame and previous points |
| 79 | + old_gray = new_gray.copy() |
| 80 | + oldTrackPoints = good_new.reshape(-1,1,2) |
| 81 | + else: |
| 82 | + break |
| 83 | + return mask |
| 84 | + |
| 85 | +def crop_image(input_img): |
| 86 | + sum_rows = np.sum(input_img, axis=0) |
| 87 | + nonnull = np.nonzero(sum_rows)[0] |
| 88 | + left = nonnull[0] |
| 89 | + right = nonnull[len(nonnull)-1] |
| 90 | + sum_cols = np.sum(input_img, axis=1) |
| 91 | + nonnull = np.nonzero(sum_cols)[0] |
| 92 | + low = nonnull[0] |
| 93 | + high = nonnull[len(nonnull)-1] |
| 94 | + return input_img[low:high,left:right] |
| 95 | + |
| 96 | + |
| 97 | +def normalize_img(mask_img): |
| 98 | + print "$$$ Be good, my beach" |
| 99 | + normalized_img = cv2.cvtColor(mask_img, cv2.COLOR_BGR2GRAY) |
| 100 | + _,thresh_img = cv2.threshold(normalized_img, 0, 255, cv2.THRESH_BINARY) |
| 101 | + kernel = np.ones((5,5),dtype=np.uint8) |
| 102 | + dilated_img = cv2.dilate(thresh_img, kernel) |
| 103 | + dilated_img = crop_image(dilated_img) |
| 104 | + height, width = dilated_img.shape |
| 105 | + dilated_img = cv2.resize(dilated_img, (800,1143)) |
| 106 | + # delta_y = (1143 - dilated_img.shape[0])/2 |
| 107 | + # delta_y = delta_y if delta_y > 0 else 0 |
| 108 | + # delta_x = (808 - dilated_img.shape[1])/2 |
| 109 | + # delta_x = delta_x if delta_x > 0 else 0 |
| 110 | + # dilated_img = cv2.copyMakeBorder(dilated_img,delta_y,delta_y,delta_x,delta_x,cv2.BORDER_CONSTANT,value=0) |
| 111 | + return dilated_img |
| 112 | + |
| 113 | + |
| 114 | +def detectIt(video, letter): |
| 115 | + cap = cv2.VideoCapture(video) |
| 116 | + try: |
| 117 | + #slow_motion_delay in milliseconds |
| 118 | + slow_motion_delay = 80 |
| 119 | + print "$$$~ Brick Squad Monopoly security ~$$$" |
| 120 | + mask_img = videoTracking(cap, slow_motion_delay, 'FAST') |
| 121 | + print "$$$ U can't hide u can't run." |
| 122 | + cap.release() |
| 123 | + cv2.imshow('mask_img', mask_img) |
| 124 | + normalized_img = normalize_img(mask_img) |
| 125 | + print "$$$ Here we watch ya." |
| 126 | + let_symbol = cv2.imread("symbol_{0}.jpg".format(letter)) |
| 127 | + let_symbol = cv2.cvtColor(let_symbol, cv2.COLOR_BGR2GRAY) |
| 128 | + _, let_symbol = cv2.threshold(let_symbol, 0, 255, cv2.THRESH_BINARY_INV) |
| 129 | + let_symbol = crop_image(let_symbol) |
| 130 | + _,let_symbol = cv2.threshold(let_symbol, 0, 255, cv2.THRESH_BINARY_INV) |
| 131 | + #Good size for us |
| 132 | + let_symbol = cv2.resize(let_symbol,(800,1143)) |
| 133 | + # cv2.imshow('let_symbol', let_symbol) |
| 134 | + # cv2.imshow('normalized_img', normalized_img) |
| 135 | + result_img = cv2.bitwise_and(normalized_img, normalized_img, mask=let_symbol) |
| 136 | + # cv2.imshow('result_img', result_img) |
| 137 | + # cv2.waitKey(0) |
| 138 | + # cv2.destroyAllWindows() |
| 139 | + if np.sum(result_img) < 0.15 * 255 * 800 * 1143: |
| 140 | + print "$$$ Br0! You are masta!" |
| 141 | + return True |
| 142 | + print "$$$ Waka Fl0cka doesn't like it! Get the float of the property!" |
| 143 | + return False |
| 144 | + except: |
| 145 | + print "$$$ Get OpenCV 3 or die tryin'." |
| 146 | + cap.release() |
| 147 | + return False |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + #Test purposes |
| 152 | + filename = "test_L.avi" |
| 153 | + detectIt(filename, "L") |
0 commit comments