def update_mhi(img, dst, diff_threshold): global last global mhi global storage global mask global orient global segmask timestamp = time.clock() / CLOCKS_PER_SEC # get current time in seconds size = cv.GetSize(img) # get current frame size idx1 = last global var1 if not mhi or cv.GetSize(mhi) != size: for i in range(N): buf[i] = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1) cv.Zero(buf[i]) mhi = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1) cv.Zero(mhi) # clear MHI at the beginning orient = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1) segmask = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1) mask = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1) cv.CvtColor(img, buf[last], cv.CV_BGR2GRAY) # convert frame to grayscale idx2 = (last + 1) % N # index of (last - (N-1))th frame last = idx2 silh = buf[idx2] cv.AbsDiff(buf[idx1], buf[idx2], silh) # get difference between frames cv.Threshold(silh, silh, diff_threshold, 1, cv.CV_THRESH_BINARY) # and threshold it cv.UpdateMotionHistory(silh, mhi, timestamp, MHI_DURATION) # update MHI cv.CvtScale(mhi, mask, 255. / MHI_DURATION, (MHI_DURATION - timestamp) * 255. / MHI_DURATION) cv.Zero(dst) cv.Merge(mask, None, None, None, dst) cv.CalcMotionGradient(mhi, mask, orient, MAX_TIME_DELTA, MIN_TIME_DELTA, 3) if not storage: storage = cv.CreateMemStorage(0) seq = cv.SegmentMotion(mhi, segmask, storage, timestamp, MAX_TIME_DELTA) for (area, value, comp_rect) in seq: if comp_rect[2] + comp_rect[3] > 250: # reject very small components color = cv.CV_RGB(255, 0, 0) silh_roi = cv.GetSubRect(silh, comp_rect) mhi_roi = cv.GetSubRect(mhi, comp_rect) orient_roi = cv.GetSubRect(orient, comp_rect) mask_roi = cv.GetSubRect(mask, comp_rect) angle = 360 - cv.CalcGlobalOrientation( orient_roi, mask_roi, mhi_roi, timestamp, MHI_DURATION) print angle count = cv.Norm( silh_roi, None, cv.CV_L1, None) # calculate number of points within silhouette ROI if count < (comp_rect[2] * comp_rect[3] * 0.05): continue magnitude = 80. center = ((comp_rect[0] + comp_rect[2] / 2), (comp_rect[1] + comp_rect[3] / 2)) cv.Circle(dst, center, cv.Round(magnitude * 1.2), color, 10, cv.CV_AA, 0) print "motion detected at", center[1] if (center[1] > 320): bottom.mright3() if (center[1] < 320): bottom.mleft4() var1 = var1 + 1 if (var1 == 40): led.motion() var1 = 0 if (center[1] > 320): # print "hai" bottom.mright3() if (center[1] < 320): bottom.mleft4() print "hai" emotion.exclaim() cv.Line( dst, center, (cv.Round(center[0] + magnitude * cos(angle * cv.CV_PI / 180)), cv.Round(center[1] - magnitude * sin(angle * cv.CV_PI / 180))), color, 3, cv.CV_AA, 0)
server_socket.bind(("172.16.50.25", 9900)) server_socket.listen(5) print "TCPServe Waiting for client on port 9900" client_socket, address = server_socket.accept() print "I got a connection from ", address bottom.mid() while(1): try: data=client_socket.recv(1) if(data=="3"): bottom.mright3() elif(data=="4"): bottom.mright2() elif(data=="5"): bottom.mright1() elif(data=="6"): bottom.mid() elif(data=="7"): bottom.mleft5() elif(data=="8"): bottom.mleft4() elif(data=="9"): bottom.mleft3() print data except KeyboardInterrupt: client_socket.close()
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind(("172.16.50.25", 9900)) server_socket.listen(5) print "TCPServe Waiting for client on port 9900" client_socket, address = server_socket.accept() print "I got a connection from ", address bottom.mid() while (1): try: data = client_socket.recv(1) if (data == "3"): bottom.mright3() elif (data == "4"): bottom.mright2() elif (data == "5"): bottom.mright1() elif (data == "6"): bottom.mid() elif (data == "7"): bottom.mleft5() elif (data == "8"): bottom.mleft4() elif (data == "9"): bottom.mleft3() print data except KeyboardInterrupt: client_socket.close()
def update_mhi(img, dst, diff_threshold): global last global mhi global storage global mask global orient global segmask timestamp = time.clock() / CLOCKS_PER_SEC # get current time in seconds size = cv.GetSize(img) # get current frame size idx1 = last global var1 if not mhi or cv.GetSize(mhi) != size: for i in range(N): buf[i] = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1) cv.Zero(buf[i]) mhi = cv.CreateImage(size,cv. IPL_DEPTH_32F, 1) cv.Zero(mhi) # clear MHI at the beginning orient = cv.CreateImage(size,cv. IPL_DEPTH_32F, 1) segmask = cv.CreateImage(size,cv. IPL_DEPTH_32F, 1) mask = cv.CreateImage(size,cv. IPL_DEPTH_8U, 1) cv.CvtColor(img, buf[last], cv.CV_BGR2GRAY) # convert frame to grayscale idx2 = (last + 1) % N # index of (last - (N-1))th frame last = idx2 silh = buf[idx2] cv.AbsDiff(buf[idx1], buf[idx2], silh) # get difference between frames cv.Threshold(silh, silh, diff_threshold, 1, cv.CV_THRESH_BINARY) # and threshold it cv.UpdateMotionHistory(silh, mhi, timestamp, MHI_DURATION) # update MHI cv.CvtScale(mhi, mask, 255./MHI_DURATION, (MHI_DURATION - timestamp)*255./MHI_DURATION) cv.Zero(dst) cv.Merge(mask, None, None, None, dst) cv.CalcMotionGradient(mhi, mask, orient, MAX_TIME_DELTA, MIN_TIME_DELTA, 3) if not storage: storage = cv.CreateMemStorage(0) seq = cv.SegmentMotion(mhi, segmask, storage, timestamp, MAX_TIME_DELTA) for (area, value, comp_rect) in seq: if comp_rect[2] + comp_rect[3] > 250: # reject very small components color = cv.CV_RGB(255, 0,0) silh_roi = cv.GetSubRect(silh, comp_rect) mhi_roi = cv.GetSubRect(mhi, comp_rect) orient_roi = cv.GetSubRect(orient, comp_rect) mask_roi = cv.GetSubRect(mask, comp_rect) angle = 360 - cv.CalcGlobalOrientation(orient_roi, mask_roi, mhi_roi, timestamp, MHI_DURATION) print angle count = cv.Norm(silh_roi, None, cv.CV_L1, None) # calculate number of points within silhouette ROI if count < (comp_rect[2] * comp_rect[3] * 0.05): continue magnitude = 80. center = ((comp_rect[0] + comp_rect[2] / 2), (comp_rect[1] + comp_rect[3] / 2)) cv.Circle(dst, center, cv.Round(magnitude*1.2), color, 10, cv.CV_AA, 0) print "motion detected at",center[1] if (center[1] > 320): bottom.mright3() if (center[1] <320): bottom.mleft4() var1=var1+1 if (var1==40): led.motion() var1=0 if (center[1] > 320): # print "hai" bottom.mright3() if (center[1] <320): bottom.mleft4() print "hai" emotion.exclaim() cv.Line(dst, center, (cv.Round(center[0] + magnitude * cos(angle * cv.CV_PI / 180)), cv.Round(center[1] - magnitude * sin(angle * cv.CV_PI / 180))), color, 3, cv.CV_AA, 0)