'Using Python and Open CV to match images and execute commands in another code
So fair warning, I am new to Python. Basically This is a script that communicates to another script. The py script compares stored images to a live feed for a reasonable match to a specific location in the live feed. While the other just interprets the results and sends them to a device.
Most of the time the images are pretty consistent and it will only print once. But some on the other hand, cause it to "flood" the output as it is matching a different similarity in the same defined location. I am trying to figure out the best way to suppress multiple print notifications from that.
This is all there is to the Python side of the code, removing print statements from the Python code remedies the print flood issue, but takes away the only means to know what the state is.
import os
import cv2
import time
class GCVWorker:
def __init__(self, width, height):
os.chdir(os.path.dirname(__file__))
self.gcvdata = bytearray(255)
self.User_defined_event_1 = cv2.imread('Images/Event_Data/xy.png')
self.User_defined_event_1_b = cv2.imread('Images/Event_Data_b/xy.png')
self.User_defined_event_2 = cv2.imread('Images/Event_Data/xy.pmg')
self.User_defined_event_2_b = cv2.imread('Images/Event_Data_b/xy.png')
self.Found_User_defined_event_1 = True
self.Found_User_defined_event_1_b = True
self.Found_User_defined_event_2 = True
self.Found_User_defined_event_2_b = True
def __del__(self):
del self.gcvdata
del self.User_defined_event_1
del self.User_defined_event_1_b
del self.User_defined_event_2
del self.User_defined_event_2_b
def process(self, frame):
self.gcvdata[0] = False
self.gcvdata[1] = False
User_defined_event_1 = frame[y1:y2, x1:x2]
similar = cv2.norm(self.User_defined_event_1, User_defined_event_1)
if similar <= 3000.0 and self.User_defined_event_1:
print('User defined event 1 Detected')
self.Found_User_defined_event_1 = False
self.gcvdata[0] = True
elif similar <= 3000.0 and self.User_defined_event_1_b:
print('User defined event 1 Detected')
self.Found_User_defined_event_1_b = False
self.gcvdata[0] = True
elif similar <= 3000.0:
pass
else:
self.Found_User_defined_event_1 = True
User_defined_event_2 = frame[y1:y2, x1:x2]
similar = cv2.norm(self.User_defined_event_2, User_defined_event_2)
if similar <= 3000.0 and self.User_defined_event_2:
print('User defined event 2 Detected')
self.gcvdata[1] = True
self.User_defined_event_2_a = False
elif similar <= 3000.0 and self.User_defined_event_2_b:
print('User defined event 2 Detected')
self.gcvdata[1] = True
self.User_defined_event_2_b = False
elif similar <= 3000.0:
pass
else:
self.Found_User_defined_event_2 = True
return frame, self.gcvdata
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
