|
|
@ -1,6 +1,9 @@ |
|
|
|
import cv2 |
|
|
|
import base64 |
|
|
|
import zmq |
|
|
|
|
|
|
|
# Constant variables definition. |
|
|
|
MAJOR_VERSION = cv2.getVersionMajor() |
|
|
|
DESIRED_HEIGHT = 480 # The input image will be resized to this height, preserving its aspect ratio. |
|
|
|
BLUE_THRESHOLD = 150 # If the blue channel is bigger than this, it is considered background and removed. |
|
|
|
BINARY_THRESHOLD = 30 # If the pixel is not brighter than this, it is removed before detection. |
|
|
@ -42,7 +45,10 @@ def removeColors(img): |
|
|
|
blue[i,j,:] = 0 |
|
|
|
|
|
|
|
gray = cv2.cvtColor(blue, cv2.COLOR_BGR2GRAY) |
|
|
|
_, contours, hierarchy = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
|
|
|
if MAJOR_VERSION == 3: |
|
|
|
_, contours, hierarchy = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
|
|
|
else: |
|
|
|
contours, hierarchy = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
|
|
|
if len(contours) > 0: |
|
|
|
maxContour = max(contours, key = cv2.contourArea) |
|
|
|
x,y,w,h = cv2.boundingRect(maxContour) |
|
|
@ -55,12 +61,15 @@ 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) |
|
|
|
_, contours, hierarchy = cv2.findContours(canny, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) |
|
|
|
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))) |
|
|
|
contours.sort(key = cv2.contourArea, reverse = True) |
|
|
|
|
|
|
|
cv2.imshow('hey', canny) |
|
|
|
#cv2.imshow('hey', canny) |
|
|
|
for i in range(len(contours)): |
|
|
|
contour = contours[i] |
|
|
|
x,y,w,h = cv2.boundingRect(contour) |
|
|
@ -93,7 +102,10 @@ def processImage(img): |
|
|
|
dilated = cv2.dilate(imgThres, (DILATION_KERNEL_SIZE,DILATION_KERNEL_SIZE), iterations=DILATION_ITERATIONS) |
|
|
|
|
|
|
|
# Find the largest image contour. |
|
|
|
_, contours, hierarchy = cv2.findContours(dilated, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) |
|
|
|
if MAJOR_VERSION == 3: |
|
|
|
_, contours, hierarchy = cv2.findContours(dilated, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) |
|
|
|
else: |
|
|
|
contours, hierarchy = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
|
|
|
if len(contours) > 0: |
|
|
|
maxContour = max(contours, key = cv2.contourArea) |
|
|
|
else: |
|
|
@ -145,29 +157,41 @@ def determinateDir(cenX, cenY, objX, objY): |
|
|
|
|
|
|
|
##################################################################################### |
|
|
|
|
|
|
|
context = zmq.Context() |
|
|
|
|
|
|
|
socket = context.socket(zmq.REP) |
|
|
|
socket.bind("tcp://*:4444") |
|
|
|
while True: |
|
|
|
client_ip = socket.recv() |
|
|
|
break |
|
|
|
|
|
|
|
footage_socket = context.socket(zmq.PUB) |
|
|
|
footage_socket.connect('tcp://' + client_ip.decode() + ':5555') |
|
|
|
|
|
|
|
#camera = cv2.VideoCapture(0) # init the camera |
|
|
|
|
|
|
|
|
|
|
|
cap = cv2.VideoCapture('/home/stelios/Desktop/drone_flight_test (cut).mp4') |
|
|
|
|
|
|
|
cap = cv2.VideoCapture('C:/Users/Giorgos Ger/Desktop/drone_test.mp4') |
|
|
|
if (cap.isOpened() == False): |
|
|
|
print('Error opening stream.') |
|
|
|
quit() |
|
|
|
|
|
|
|
#cap.set(1, 30*6) |
|
|
|
|
|
|
|
while(cap.isOpened()): |
|
|
|
ret, frame = cap.read() |
|
|
|
if (ret == True): |
|
|
|
img, xDir, yDir = processImage(frame) |
|
|
|
cv2.imshow('Frame', img) |
|
|
|
|
|
|
|
print('Got ' + str(xDir) + ' ' + str(yDir)) |
|
|
|
|
|
|
|
k = cv2.waitKey(25) & 0xFF |
|
|
|
if k == 27: |
|
|
|
while (cap.isOpened()): |
|
|
|
try: |
|
|
|
ret, frame = cap.read() |
|
|
|
if (ret == True): |
|
|
|
img, xDir, yDir = processImage(frame) |
|
|
|
#cv2.imshow('Frame', img) |
|
|
|
encoded, buffer = cv2.imencode('.jpg', img) |
|
|
|
jpg_as_text = base64.b64encode(buffer) |
|
|
|
footage_socket.send(jpg_as_text) |
|
|
|
cv2.waitKey(33) |
|
|
|
else: |
|
|
|
break |
|
|
|
if k == ord('p') or k == ord('P'): |
|
|
|
cv2.waitKey(0) |
|
|
|
|
|
|
|
else: |
|
|
|
break |
|
|
|
cap.release() |
|
|
|
cv2.destroyAllWindows() |
|
|
|
except KeyboardInterrupt: |
|
|
|
cap.release() |
|
|
|
cv2.destroyAllWindows() |
|
|
|
break |