From f4d4326e2db8c721052c1e183b435c14b6fff93f Mon Sep 17 00:00:00 2001 From: Stelios Giakoumidis Date: Wed, 22 Jan 2020 11:05:21 +0200 Subject: [PATCH] Added comments and removed debug elements from the python code. --- camera module/main.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/camera module/main.py b/camera module/main.py index 1487b3b..c20bb98 100644 --- a/camera module/main.py +++ b/camera module/main.py @@ -38,25 +38,24 @@ def resizeImage(img): def findMatchingContour(img, objX, objY): - dilated = img.copy() - #dilated = cv2.dilate(img, (5,5), iterations=1) - canny = cv2.Canny(dilated, CANNY_LOW_THRES, CANNY_HIGH_THRES) + # Apply the canny detector on the image and find the contours. + canny = cv2.Canny(img, CANNY_LOW_THRES, CANNY_HIGH_THRES) if MAJOR_VERSION == 3: _, contours, hierarchy = cv2.findContours(canny, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) else: contours, hierarchy = cv2.findContours(canny, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) - #print('len:' + str(len(contours))) + # Sort the contours based on their area. contours.sort(key = cv2.contourArea, reverse = True) - #cv2.imshow('hey', canny) + # Determine which of the detected contours is the drone. for i in range(len(contours)): contour = contours[i] x,y,w,h = cv2.boundingRect(contour) area = w*h dist = cv2.pointPolygonTest(contour, (objX,objY), False) - #print('dist: ' + str(dist)) + if area >= MIN_CONTOUR_AREA and area <= MAX_CONTOUR_AREA and dist >= 0: return (True, contour) @@ -66,7 +65,6 @@ def processImage(img): # Resize image to the desired height. resized = resizeImage(img) - #return removeColors(resized) dim = resized.shape # Convert to grayscale. @@ -100,18 +98,16 @@ def processImage(img): objCenterY = int( (y + y + h) / 2) imgCenterX = int(dim[1]/2) imgCenterY = int(dim[0]/2) - #cv2.circle(resized, (objCenterX, objCenterY), 5, BLUE, LINE_THICKNESS) + ret, finalContour = findMatchingContour(blur, objCenterX, objCenterY) if (ret == False): return (resized, 0, 0) - #cv2.fillPoly(resized, finalContour, BLUE, cv2.LINE_4) x,y,w,h = cv2.boundingRect(finalContour) objCenterX = int( (x + x + w) / 2) objCenterY = int( (y + y + h) / 2) # Draw the bounding rectangle and its centroid to the image. - #cv2.circle(resized, (objCenterX, objCenterY), 5, YELLOW, LINE_THICKNESS) cv2.rectangle(resized, (x,y), (x+w,y+h), RED, LINE_THICKNESS) cv2.line(resized, (objCenterX, objCenterY), (imgCenterX, imgCenterY), YELLOW, LINE_THICKNESS) @@ -159,15 +155,14 @@ cap = cv2.VideoCapture(0) if (cap.isOpened() == False): print('Error opening stream.') quit() - -#cap.set(1, 30*6) while (cap.isOpened()): try: ret, frame = cap.read() if (ret == True): img, xDir, yDir = processImage(frame) - #cv2.imshow('Frame', img) + + # Encode and send the processed frame. encoded, buffer = cv2.imencode('.jpg', img) jpg_as_text = base64.b64encode(buffer) footage_socket.send(jpg_as_text)