Major refactoring for better usability
1. Created interface classes for reading video streams (in imagereaders.py) 2. Created a class for ball detection (for reusability) 3. Reworked colorpicker, now it is possible to choose the mode of operation from command line (available are live from Nao, from video file or from webcam). It is possible to capture only the first image of a stream and work on it. Colorpicker now can save the settings on exit or load settings on startup.
This commit is contained in:
57
scripts/imagereaders.py
Normal file
57
scripts/imagereaders.py
Normal file
@@ -0,0 +1,57 @@
|
||||
import numpy as np
|
||||
import cv2
|
||||
# from naoqi import ALProxy
|
||||
ALProxy = None
|
||||
|
||||
|
||||
class NaoImageReader(object):
|
||||
|
||||
RESOLUTIONS = {
|
||||
1: (240, 320),
|
||||
2: (480, 640),
|
||||
3: (960, 1280)
|
||||
}
|
||||
|
||||
def __init__(self, ip, port=9559, res=1, fps=30, cam_id=0):
|
||||
self.res = self.RESOLUTIONS[res]
|
||||
self.vd = ALProxy('ALVideoDevice', ip, port)
|
||||
self.sub = self.vd.subscribeCamera(
|
||||
"video_streamer", cam_id, res, 13, fps
|
||||
)
|
||||
|
||||
def get_frame(self):
|
||||
result = self.vd.getImageRemote(self.sub)
|
||||
self.vd.releaseImage(self.sub)
|
||||
if result == None:
|
||||
raise RuntimeError('cannot capture')
|
||||
elif result[6] == None:
|
||||
raise ValueError('no image data string')
|
||||
else:
|
||||
height, width = self.res
|
||||
return np.frombuffer(result[6], dtype=np.uint8).reshape(
|
||||
height, width, 3
|
||||
)
|
||||
|
||||
def close(self):
|
||||
self.vd.unsubscribe(self.sub)
|
||||
|
||||
|
||||
class VideoReader(object):
|
||||
|
||||
def __init__(self, filename=0, loop=False):
|
||||
self.cap = cv2.VideoCapture(filename)
|
||||
self.loop = loop if filename else False
|
||||
self.ctr = 0
|
||||
|
||||
def get_frame(self):
|
||||
succ, frame = self.cap.read()
|
||||
if not succ:
|
||||
raise ValueError('Error while reading video')
|
||||
self.ctr += 1
|
||||
if self.ctr == self.cap.get(cv2.CAP_PROP_FRAME_COUNT) and self.loop:
|
||||
self.ctr = 0
|
||||
self.cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
|
||||
return frame
|
||||
|
||||
def close(self):
|
||||
self.cap.release()
|
||||
Reference in New Issue
Block a user