From ff81808d63c0420c9278f2fa372ec56cdfb32b5d Mon Sep 17 00:00:00 2001 From: Hobe Date: Wed, 2 Oct 2019 11:55:17 +0000 Subject: [PATCH] single object detection git-svn-id: http://newslabx.csie.ntu.edu.tw/svn/Ginger@38 5747cdd2-2146-426f-b2b0-0570f90b98ed --- trunk/code/detect_object.py | 73 ++++++++++++++++++++++++++++++++++++++++++ trunk/code/output.avi | Bin 0 -> 5686 bytes trunk/code/readGridEyeData.py | 21 ++++++++++-- 3 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 trunk/code/detect_object.py create mode 100644 trunk/code/output.avi diff --git a/trunk/code/detect_object.py b/trunk/code/detect_object.py new file mode 100644 index 0000000..1f73891 --- /dev/null +++ b/trunk/code/detect_object.py @@ -0,0 +1,73 @@ +import readGridEyeData + +import sys +import numpy as np +import cv2 + +SIZE = 8 +visible_size = 128 +distanceBetweenSensors_w = 2.6 #cm +distanceBetweenSensors_h = 2.6 #cm +distance2Object = 60.0 #cm +OFFSET = ((0, 0), ()) + +TEMP_SCALE = 20 +RED = (0, 0, 255) +GREEN = (0, 255, 0) +BLUE = (255, 0, 0) + +background = readGridEyeData.GridEyeData(sys.argv[1]) + +background_frames = [np.zeros((SIZE,SIZE)), np.zeros((SIZE,SIZE)), + np.zeros((SIZE,SIZE)), np.zeros((SIZE,SIZE))] +cnt = 1 +Horizon_array = np.array([[x for x in range(8)] for _ in range(8)]) +Vertical_array = np.array([[x for _ in range(8)] for x in range(8)]) + +# Gets background of each pixel. +while background.readFrame(): + frames = background.frames + for i in range(4): + background_frames[i] += np.array(frames[i].data) + cnt += 1 + +for i in range(4): + background_frames[i]/=cnt + +# Reads data from object frames. +objects = readGridEyeData.GridEyeData(sys.argv[2]) + +while objects.readFrame(): + frames = objects.frames + imgs = [] + heat_points = [] + + # Subtracts background and scale the value. + for i in range(4): + frame = np.array(frames[i].data) - background_frames[i] + frame[frame < 0] = 0.0 + frame *= TEMP_SCALE + frame[frame > 255] = 255 + imgs.append(frame.astype(np.uint8)) + x = int(sum(sum(frame * Horizon_array)) / sum(sum(frame)) / (SIZE - 1) * + (visible_size - 1)) + y = int(sum(sum(frame * Vertical_array)) / sum(sum(frame)) / (SIZE - 1) * + (visible_size - 1)) + heat_points.append((x,y)) + print (x, y) + all_img = np.concatenate((np.concatenate((imgs[0], imgs[1]), axis = 1), + np.concatenate((imgs[2], imgs[3]), axis = 1)), + axis = 0) + all_img = cv2.cvtColor(all_img, cv2.COLOR_GRAY2RGB) + all_img = cv2.resize(all_img, (visible_size*2, visible_size*2), + interpolation = cv2.INTER_NEAREST) + cv2.circle(all_img, heat_points[0], 5, RED, -1) + cv2.circle(all_img, (heat_points[1][0]+visible_size, heat_points[1][1]), 5, RED, -1) + + cv2.imshow('sample', all_img) + + + while cv2.getWindowProperty('sample', 0) >= 0: + key = cv2.waitKey(50) + if key >= 0: + break diff --git a/trunk/code/output.avi b/trunk/code/output.avi new file mode 100644 index 0000000000000000000000000000000000000000..f1289b6b8aa0bfa05ac09f0f01552fea06054262 GIT binary patch literal 5686 zcmWIYbaT@aV_nr zAPlpQkwJn1Vip@*DU9J2>gO#1w3`JgU|^7v43itBM?+vV1V%$(Gz3ONU^E0qLtr!n zMnhmU1V%$(Gz3ONU^E0qMhJk$2Uvgu5BVv%IY5;0V`O+6t1m(0G*UnrPd_(*&tSI@ zJ|NpCu`JEhLeJPx&(Oe-0Tfz4n6YXe6&wwL(GVEsApj~NKqINS`DK}zDHVnw?*ag3 CH!n~C literal 0 HcmV?d00001 diff --git a/trunk/code/readGridEyeData.py b/trunk/code/readGridEyeData.py index 9a53fdb..9d02d26 100644 --- a/trunk/code/readGridEyeData.py +++ b/trunk/code/readGridEyeData.py @@ -23,6 +23,7 @@ if __name__ == '__main__': import cv2 import numpy as np import sys + from functools import reduce def exponential(img, value): tmp = cv2.pow(img.astype(np.double), value)*(255.0/(255.0**value)) return tmp.astype(np.uint8) @@ -38,6 +39,9 @@ if __name__ == '__main__': MIN_EXIST_TIME = 0.1 cnt = 0 avers = [] + + raw_aver = np.array([0]*64*4, np.float64) + raw_aver2 = np.array([0]*64*4, np.float64) fourcc = cv2.VideoWriter_fourcc(*'XVID') videoWriter = cv2.VideoWriter('output.avi', fourcc, 10.0, (SIZE*2,SIZE*4)) @@ -53,19 +57,32 @@ if __name__ == '__main__': innerStartTime = 0 path = [] speed = 0 + avers.append(np.zeros((SIZE,SIZE), np.uint16)) + avers.append(np.zeros((SIZE,SIZE), np.uint16)) + avers.append(np.zeros((SIZE,SIZE), np.uint16)) + avers.append(np.zeros((SIZE,SIZE), np.uint16)) while gridEye.readFrame(): frames = gridEye.frames imgs = [] + raw = [] for frame in frames: img = (np.array(frame.data)-15)*10 img = cv2.resize(img.astype(np.uint8), (SIZE,SIZE), interpolation = cv2.INTER_LINEAR) # INTER_LINEAR, INTER_CUBIC imgs.append(img) - avers.append(np.zeros((SIZE,SIZE), np.uint16)) + raw += reduce(lambda x,y: x+y, frame.data) + raw_aver += np.array(raw) + raw_aver2 += np.array(raw)**2 if cnt < AVERAGE_FRAME: cnt += 1 for i in range(len(imgs)): avers[i] += imgs[i] if cnt == AVERAGE_FRAME: + b = (raw_aver/AVERAGE_FRAME)**2 + a = raw_aver2/AVERAGE_FRAME + print ('aver', raw_aver/AVERAGE_FRAME) + print ((a-b)**0.5) + print (sum((a-b)**0.5)/64/4) + exit() for i in range(len(avers)): avers[i] = avers[i]/AVERAGE_FRAME avers[i] = avers[i].astype(np.uint8) @@ -227,4 +244,4 @@ if __name__ == '__main__': break videoWriter.release() - cv2.destroyAllWindows() \ No newline at end of file + cv2.destroyAllWindows()