-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCV_Diff.py
130 lines (111 loc) · 5.57 KB
/
CV_Diff.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import libjevois as jevois
import cv2
import numpy as np
import time
lastimg =None #initialize global. todo: figure out how to put in the class
lastcolorimg=None
x=1000000 #number of pixel changes before the scene is deemed to be different.
## Store changed images
#
# Write a message to the serial output anytime the image changes more than x pixels
#
# @author peter quinn
#
# @videomapping YUYV 640 480 10 YUYV 640 480 15 PDQ CV_Diff
# @email peterquinn925@gmail.com
# @address 123 first street, Los Angeles CA 90012, USA
# @copyright Copyright (C) 2018 by peter quinn
# @mainurl
# @supporturl
# @otherurl
# @license
# @distribution Unrestricted
# @restrictions None
# @ingroup modules
class CV_Diff:
# ###################################################################################################
## Constructor
def __init__(self):
# Instantiate a JeVois Timer to measure our processing framerate:
self.timer = jevois.Timer("processing timer", 100, jevois.LOG_INFO)
# a simple frame counter used to demonstrate sendSerial():
self.frame = 0
# ###################################################################################################
## Process function with no USB output
def processNoUSB(self, inframe):
# Get the next camera image (may block until it is captured) and here convert it to OpenCV BGR. If you need a
# grayscale image, just use getCvGRAY() instead of getCvBGR(). Also supported are getCvRGB() and getCvRGBA():
inimg = inframe.getCvGRAY()
# Start measuring image processing time (NOTE: does not account for input conversion time):
self.timer.start()
jevois.LINFO("Processing video frame {} now...".format(self.frame))
# TODO: you should implement some processing.
# Once you have some results, send serial output messages:
# Get frames/s info from our timer:
fps = self.timer.stop()
# Send a serial output message:
jevois.sendSerial("DONE frame {} - {}".format(self.frame, fps));
self.frame += 1
# ###################################################################################################
## Process function with USB output
def process(self, inframe, outframe):
global lastimg
global lastcolorimg
global x #num of changed pixels before considering the scene different
# Get the next camera image (may block until it is captured) and here convert it to OpenCV BGR. If you need a
# grayscale image, just use getCvGRAY() instead of getCvBGR(). Also supported are getCvRGB() and getCvRGBA():
inimg = inframe.getCvGRAY()
colorimg = inframe.getCvBGR()
if lastimg is None:
lastimg = inframe.getCvGRAY()
lastcolorimg = colorimg
# Start measuring image processing time (NOTE: does not account for input conversion time):
self.timer.start()
diffimg= cv2.absdiff(inimg,lastimg) # compare two images. Will be all black if they are the same.
totaldiff = np.sum(diffimg) #count how many pixels are not black
if totaldiff > x: #the image has changed, update it.
outimg=colorimg
lastcolorimg=colorimg
jevois.sendSerial("Frame Changed");
else:
#outimg = diffimg #use for checking the tolerances
outimg = lastcolorimg
time.sleep(.1)
lastimg = inframe.getCvGRAY()
# Write a title:
cv2.putText(outimg, "JeVois CV_Diff", (3, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))
# Write frames/s info from our timer into the edge map (NOTE: does not account for output conversion time):
fps = self.timer.stop()#not used
height = outimg.shape[0]
width = outimg.shape[1]
cv2.putText(outimg, str(totaldiff), (3, height - 6), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))
# Convert our output image to video output format and send to host over USB:
outframe.sendCv(outimg)
# Example of sending some serial output message:
# jevois.sendSerial("DONE frame {}".format(self.frame));
self.frame += 1
jevois.sendSerial("diff: {}".format(str(totaldiff)));
# ###################################################################################################
## Parse a serial command forwarded to us by the JeVois Engine, return a string
def parseSerial(self, str):
global x #the threshold pixels to trigger a change
jevois.LINFO("parseserial received command [{}]".format(str))
if str == "hello":
return self.hello()
elif "setthresh" in str:
inputline=str.split(" ")
x=int(inputline[1])
return self.setthresh()
else:
return "ERR Unsupported command"
# ###################################################################################################
## Return a string that describes the custom commands we support, for the JeVois help message
def supportedCommands(self):
# use \n seperator if your module supports several commands
return "setthresh"
# ###################################################################################################
## Internal method that gets invoked as a custom command
def hello(self):
return "Hello from python!"
def setthresh(self):
return "Threshold set"