def PlayMonitor(self_MP): try: self_MP.ROIs # if there are no ROIs yet, make an empty list except: self_MP.ROIs = [] if not gbl.genmaskflag or self_MP.ROIs == []: self_MP.ROIs = gbl.loadROIsfromMaskFile( gbl.mask_file) # new ROIs are ready self_MP.ROIframe, self_MP.redmask = gbl.makeMaskFrames( self_MP.ROIs, self_MP.initialSize ) # creates overlay of ROIs with source dimensions # each of the 3 video input classes has a "getImage" function self_MP.frame = self_MP.captureVideo.getImage() # multiply element by element to leave zeros where lines will be drawn self_MP.frame2 = np.multiply(self_MP.frame.copy(), self_MP.ROIframe) # add redmask to frame to turn lines red self_MP.frame3 = np.add(self_MP.frame2.copy(), self_MP.redmask) self_MP.newframe = cv2.resize(self_MP.frame3.copy(), dsize=self_MP.size) self_MP.bmp = wx.BitmapFromBuffer(self_MP.size[0], self_MP.size[1], self_MP.newframe.tostring()) self_MP.keepPlaying = True self_MP.Show() gbl.genmaskflag = False
def setMonitor_old(self, camera, resolution=None): """ """ if not resolution: resolution = self.size self.camera = camera self.resolution = resolution if not self.mon: self.mon = pysolovideo.Monitor() self.mon.setSource(self.camera, self.resolution) frame = self.mon.GetImage(drawROIs=self.drawROI, selection=self.selection, crosses=self.polyPoints, timestamp=self.timestamp) self.bmp = wx.BitmapFromBuffer(self.size[0], self.size[1], frame) self.Bind(wx.EVT_PAINT, self.onPaint) self.playTimer = wx.Timer(self) self.Bind(wx.EVT_TIMER, self.onNextFrame) if self.keymode: self.Bind(wx.EVT_CHAR, self.onKeyPressed) self.SetFocus()
def run(self): print 'starting srteamer...' if self.__proxysetting is not None: proxy = urllib2.ProxyHandler(self.__proxysetting) opener = urllib2.build_opener(proxy) urllib2.install_opener(opener) while not self.__stop: time.sleep(5.0) print('opening stream at %s ...' % (self.__url)) self.LogString('opening stream at %s ...' % (self.__url)) self.stream = None try: self.stream = urllib2.urlopen(self.__url, timeout=10.0) print 'stream opened' self.LogString('stream opened') except URLError as e: print e.reason continue except socket.timeout as e: print("timeout") self.LogErrorString('stream timeout') continue socket.setdefaulttimeout(5.0) self.frame = self.loadimg() if self.frame is not None: self.height, self.width = self.frame.shape[:2] self.bmp = wx.BitmapFromBuffer(self.width, self.height, self.frame) else: print "Error no webcam image" self.LogErrorString('no webcam image') continue while not self.__stop and self.frame is not None: self.detectthread.augment(self.frame) self.lock() self.bmp.CopyFromBuffer(self.frame) self.unlock() #print "Fire event" event = RedrawEvent(bmp=self.bmp) wx.PostEvent(self.wnd, event) time.sleep(0.05) self.frame = self.loadimg() if self.frame is not None and self.detectthread is not None: self.detectthread.putframe(self.frame) #if self.cvNet is not None: # self.frame = self.obj_detect(self.frame) self.stream.close() print "Streamer stopped"
def __init__(self, parent, title="View Window"): # super(viewWindow,self).__init__(parent) wx.Frame.__init__(self, parent) self.imgSizer = (640, 640) self.pnl = wx.Panel(self) self.vbox = wx.BoxSizer(wx.VERTICAL) self.image = wx.EmptyImage(self.imgSizer[0], self.imgSizer[1]) self.imageBit = wx.BitmapFromImage(self.image) self.staticBit = wx.StaticBitmap(self.pnl, wx.ID_ANY, self.imageBit) self.vbox.Add(self.staticBit) self.capture = cv2.VideoCapture(0) ret, self.frame = self.capture.read() if ret: self.height, self.width = self.frame.shape[:2] self.bmp = wx.BitmapFromBuffer(self.width, self.height, self.frame) self.timex = wx.Timer(self) self.timex.Start(1000. / 24) self.Bind(wx.EVT_TIMER, self.redraw) self.SetSize(self.imgSizer) else: print "Error no webcam image" self.pnl.SetSizer(self.vbox) self.vbox.Fit(self) self.Show()
def _makeColourMapBitmap(cmap): """Used by the :func:`_ColourMap` function. Makes a little bitmap image from a :class:`~matplotlib.colors.Colormap` instance. """ import numpy as np width, height = 75, 15 # create a single colour for each horizontal pixel colours = cmap(np.linspace(0.0, 1.0, width)) # discard alpha values colours = colours[:, :3] # repeat each horizontal pixel (height) times colours = np.tile(colours, (height, 1, 1)) # scale to [0,255] and cast to uint8 colours = colours * 255 colours = np.array(colours, dtype=np.uint8) # make a wx Bitmap from the colour data colours = colours.ravel(order='C') if six.PY2: bitmap = wx.BitmapFromBuffer(width, height, colours) else: bitmap = wx.Bitmap.FromBuffer(width, height, colours) return bitmap
def cvIplImageAsBitmap(self, flip=True): flags = CV_CVTIMG_SWAP_RB if flip: flags |= CV_CVTIMG_FLIP cvConvertImage(self, self, flags) return wx.BitmapFromBuffer(self.width, self.height, self.data_as_string())
def __init__(self, parent, img_path): wx.Panel.__init__(self, parent) hbox = wx.BoxSizer(wx.HORIZONTAL) self.origin_img = cv2.imread(img_path) height, width = self.origin_img.shape[:2] self.SetSize((height * 3, width * 3)) self.rbg_img = cv2.cvtColor(self.origin_img, cv2.COLOR_BGR2RGB) self.bmp = wx.BitmapFromBuffer(width, height, self.rbg_img) self.edge_img = wx.StaticBitmap(self, size=(width, height)) self.edge_img.SetBitmap(self.bmp) hbox.Add(self.edge_img, 1) hbox.Add((-1, -1), 1) self.binary_img = wx.StaticBitmap(self, size=(width, height)) self.binary_img.SetBitmap(self.bmp) hbox.Add(self.binary_img) self.sp = 5 self.sr = 5 self.threshold = 20 self.w = width self.h = height self.cv_bmp = None self.SetSizer(hbox)
def __init__(self, capture, title=None, parent=None, id=-1, fps=10): """Class constructor This method initializes all necessary parameters and generates a basic GUI layout that can then be modified by self.init_custom_layout() and self.create_custom_layout(). :param parent: A wx.Frame parent (often Null). If it is non-Null, the frame will be minimized when its parent is minimized and restored when it is restored. :param id: The window identifier. Value -1 indicates default value. :param title: The caption to be displayed on the frame's title bar. :param capture: A cv2.VideoCapture object to be used as camera feed. :param fps: frames per second at which to display camera feed """ self.capture = capture self.fps = fps # determine window size and init wx.Frame success, frame = self._acquire_frame() if not success: print "Could not acquire frame from camera." raise SystemExit self.imgHeight, self.imgWidth = frame.shape[:2] self.bmp = wx.BitmapFromBuffer(self.imgWidth, self.imgHeight, frame) wx.Frame.__init__(self, parent, id, title, size=(self.imgWidth, self.imgHeight)) self._init_base_layout() self._create_base_layout()
def __init__(self, parent, id, title, capture, fps=15): # initialize screen capture self.capture = capture # determine window size and init wx.Frame frame, _ = freenect.sync_get_depth() self.imgHeight, self.imgWidth = frame.shape[:2] buffer = np.zeros((self.imgWidth, self.imgHeight, 3), np.uint8) self.bmp = wx.BitmapFromBuffer(self.imgWidth, self.imgHeight, buffer) wx.Frame.__init__(self, parent, id, title, size=(self.imgWidth, self.imgHeight)) # set up periodic screen capture self.timer = wx.Timer(self) self.timer.Start(1000. / fps) self.Bind(wx.EVT_TIMER, self.NextFrame) # counteract flicker def disable_event(*pargs, **kwargs): pass self.Bind(wx.EVT_ERASE_BACKGROUND, disable_event) # create the layout, which draws all buttons and # connects events to class methods self.CreateLayout()
def threshold_img(self): if self.cv_bmp is not None: gray_img = cv2.cvtColor(self.cv_bmp, cv2.COLOR_BGR2GRAY) ret, mask = cv2.threshold(gray_img, self.threshold, 255, cv2.THRESH_BINARY) ret, mask_inv = cv2.threshold(gray_img, self.threshold, 255, cv2.THRESH_BINARY_INV) # emptyImage = np.zeros(self.cv_bmp.shape, np.uint8) # emptyImage[:] cv2.imwrite(path + "np_empty_img.jpg", mask) # img = cv2.bitwise_and(self.cv_bmp, self.cv_bmp, mask=mask) # bmp = wx.BitmapFromBuffer(self.w, self.h, img) # self.binary_img.SetBitmap(bmp) lines = cv2.HoughLinesP(mask, rho=1, theta=1 * np.pi / 180, threshold=100, minLineLength=100, maxLineGap=80) img = self.cv_bmp.copy() for x1, y1, x2, y2 in lines[0]: cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 2) bmp = wx.BitmapFromBuffer(self.w, self.h, img) self.binary_img.SetBitmap(bmp) # how to set thresh gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(gray_img, 120, 255, 1) # 寻找轮廓 contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) max_c = contours[0] for c in contours: c = contours[0] hull = cv2.convexHull(c) # 10%,即0.1的精确度 epsilon = 0.1 * cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, epsilon, True) print approx cv2.drawContours(img, [approx], -1, (0, 0, 255), 3) cv2.imwrite(path + "recteng.jpg", img)
def onPaint(self, event): if self.feed and self.playing: fw, fh = self.video_frame.GetSize() frame = self.feed.next_frame(fw, fh) if frame is None: if self.repeat: self.feed.set_position(0) frame = self.feed.next_frame(fw, fh) else: self.Toggle_Play() return h, w = frame.shape[:2] try: image = wx.BitmapFromBuffer(w, h, frame) except Exception: image = wx.Bitmap.FromBuffer(w, h, frame) dc = wx.BufferedPaintDC(self.video_frame) dc.DrawBitmap(image, 0, 0) self.slider.Value = self.feed.get_position() else: self.SetBackgroundColour( 'White') # TODO: разобраться с этим костылём event.Skip()
def _showImage(self, image): if self._nightMode or self._modeOverride: if self._currentBitmapDayNightMode != 0: self._bitmapDayNightMode.SetBitmap(self._BITMAP_NIGHT) self._currentBitmapDayNightMode = 0 if sum(self._lightStates) > 0: if self._currentBitmapLightState != 1: self._pi.set_PWM_dutycycle(18, 0) self._bitmapLightState.SetBitmap(self._BITMAP_DIM) self._currentBitmapLightState = 1 else: if self._currentBitmapLightState != 2: self._pi.set_PWM_dutycycle(18, 255) self._bitmapLightState.SetBitmap(self._BITMAP_BRIGHT) self._currentBitmapLightState = 2 else: if self._currentBitmapDayNightMode != 1: self._bitmapDayNightMode.SetBitmap(self._BITMAP_DAY) self._currentBitmapDayNightMode = 1 if self._currentBitmapLightState != 0: self._bitmapLightState.SetBitmap(self._BITMAP_OFF) self._currentBitmapLightState = 0 if image is None: # Provide a black bitmap. self._bitmapCapture.SetBitmap(self._BITMAP_NONE) else: # Convert the image to bitmap format. #image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) h, w = image.shape[:2] bitmap = wx.BitmapFromBuffer(w, h, image) # Show the bitmap. self._bitmapCapture.SetBitmap(bitmap)
def setupCapture(self): """ Creates the camera capture object used to retrieve new frames """ try: # create a cameraCapture object using OpenCV self.cameraCapture = cv2.VideoCapture(self.cameraIndex) # get the current frame, convert colours and store ret, frame = self.cameraCapture.read() height, width = frame.shape[:2] frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # update the internal bitmap with the current frame and update the display self.bmp = wx.BitmapFromBuffer(width, height, frame) self.videoFeed.SetBitmap(self.bmp) # resize the feed panel to fit everything self.videoFeedPanel.Layout() self.videoFeedPanel.SetFocus() # set the video feed as open self.feedOn = True except AttributeError: self.feedOn = False wx.MessageBox('Could not start video feed!', 'Error', wx.OK | wx.ICON_ERROR)
def __init__(self, parent, fps=15): wx.Panel.__init__(self, parent, size=(640, 400)) #self.capture = capture #ret, frame = self.capture.read() # keyboard bind self.down_keys = 0 self.pub = rospy.Publisher('car_control_manual', String, queue_size=10) self.Bind(wx.EVT_KEY_DOWN, self.on_key_down) self.Bind(wx.EVT_KEY_UP, self.on_key_up) # Image bind self.img_sub = rospy.Subscriber('image_publisher', String, self.img_callback, queue_size=1) self.cap_que = Queue.Queue(maxsize=10) self.bmp = wx.BitmapFromBuffer(640, 480, self.cap_que.get()) self.Bind(wx.EVT_PAINT, self.on_paint) #height, width = frame.shape[:2] #frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) self.timer = wx.Timer(self) self.timer.Start(200.0 / fps) self.Bind(wx.EVT_TIMER, self.next_frame)
def __init__(self, parent, id, title, capture, fps=15): # initialize screen capture self.capture = capture # determine window size and init wx.Frame ret, frame = self.capture.read() self.imgHeight, self.imgWidth = frame.shape[:2] buffer = np.zeros((self.imgWidth, self.imgHeight, 3), np.uint8) self.bmp = wx.BitmapFromBuffer(self.imgWidth, self.imgHeight, buffer) wx.Frame.__init__(self, parent, id, title, size=(self.imgWidth, self.imgHeight)) # counteract flicker def disable_event(*pargs, **kwargs): pass self.Bind(wx.EVT_ERASE_BACKGROUND, disable_event) self.CreateLayout() self.InitializeAlgorithm() # set up periodic screen capture self.timer = wx.Timer(self) self.timer.Start(1000. / fps) self.Bind(wx.EVT_TIMER, self.NextFrame)
def toWxBitmap(img): h, w = img.shape[:2] try: image = wx.Bitmap.FromBuffer(w, h, img) except Exception: image = wx.BitmapFromBuffer(w, h, img) return image
def __init__(self, parent, size=(640, 640), fps=15): self.doprint = False wx.Panel.__init__(self, parent) self.mode = 'test' self.size = size self.start = time.time() self.ResetImage() height, width = size parent.SetSize((width, height)) self.bmp = wx.BitmapFromBuffer(width, height, self.img) self.timer = wx.Timer(self) self.timer.Start(1000. / fps) self.episode = 0 self.answers = [] self.c = '' self.Bind(wx.EVT_PAINT, self.OnPaint) self.Bind(wx.EVT_TIMER, self.NextFrame) self.Bind(wx.EVT_KEY_DOWN, self.KeyDown)
def takeNewImage(self): if cv != None: frame = cv.QueryFrame(self._cam) cv.CvtColor(frame, frame, cv.CV_BGR2RGB) self._bitmap = wx.BitmapFromBuffer(frame.width, frame.height, frame.imageData) elif win32vidcap != None: buffer, width, height = self._cam.getbuffer() wxImage = wx.EmptyImage(width, height) wxImage.SetData(buffer[::-1]) if self._bitmap != None: del self._bitmap self._bitmap = wxImage.ConvertToBitmap() del wxImage del buffer if self._doTimelaps: filename = os.path.normpath( os.path.join( os.path.split(__file__)[0], "../__tmp_snap", "__tmp_snap_%04d.jpg" % (self._snapshotCount))) self._snapshotCount += 1 self._bitmap.SaveFile(filename, wx.BITMAP_TYPE_JPEG) return self._bitmap
def __init__(self, ancestor, _id, window_name, capture, frame_per_second=10): self.capture = capture self.fps = frame_per_second res, frame = self.get_frame() self.m_frame = frame.copy() if not res: print "Не могу показать кадр из камеры" raise SystemExit # Определение высоты и ширины изображения self.imgH, self.imgW = frame.shape[:2] # Получить BitMap self.bmp = wx.BitmapFromBuffer(self.imgW, self.imgH, frame) wx.Frame.__init__(self, ancestor, _id, window_name, size=(self.imgW, self.imgH)) self._init_base_layout() self._create_base_layout() self.cnt = 0
def pil_image_to_scaled_image(pil_image, desired_width): bytes_ = [item for points in pil_image.getdata() for item in points] width, height = pil_image.size aspect_ratio = float(height)/width bm = wx.BitmapFromBuffer(width, height, array.array('B', bytes_)) full_image = wx.ImageFromBitmap(bm) scaled_image = full_image.Scale(desired_width, int(desired_width * aspect_ratio), wx.IMAGE_QUALITY_NORMAL) return wx.BitmapFromImage(scaled_image)
def bufferToBitmap(data): w, h, comps = data.shape if comps == 4: return wx.BitmapFromBufferRGBA(w, h, data) elif comps == 3: return wx.BitmapFromBuffer(w, h, data) else: raise
def GetWxBitmap(self): (width, height) = self._size if self._format == wx.BitmapBufferFormat_RGB: return wx.BitmapFromBuffer(width, height, self._GetData()) else: return wx.BitmapFromBufferRGBA(width, height, self._GetData())
def onSize(self, event): if self.img != None: width, height = self.GetClientSizeTuple() img2 = cv.CreateMat(height, width, self.img.type) cv.Resize(self.img, img2, cv.CV_INTER_NN) self.bmp = wx.BitmapFromBuffer(img2.width, img2.height, img2.tostring()) self.Refresh()
def connectServer(self, address, port): if Tcp.connectServer(self, address, port): self.frame = self.recvFrame() height, width = self.frame.shape[:2] self.bmp = wx.BitmapFromBuffer(width, height, self.frame) return True else: return False
def callback(self,in_data, frame_count, time_info, status): start = self.sample_pos*CHUNK stop = (self.sample_pos+1)*CHUNK # get data from microphone data = np.float32(np.fromstring(in_data, 'Int16')) self.sample_buffer[start:stop] = data self.sample_pos = self.sample_pos+1 if self.sample_pos == NUM_WINDOWS: self.sample_pos = 0 # if this is the first time through, we record the average power/hz to use for thresholding if self.in_setup: freq,psd = sig.welch(np.roll(self.sample_buffer,-self.sample_pos*CHUNK), fs=Fs, nfft=2*SIZE, scaling='spectrum') self.threshold = np.mean(psd) + NUM_SIGMA*np.std(psd) print self.threshold self.in_setup = False # we need to wait until we have gathered at least NUM_WINDOWS # buffers, so we don't divide by zero if self.in_setup: return (None, pyaudio.paContinue) # calculate psd freq,psd = sig.welch(np.roll(self.sample_buffer,-self.sample_pos*CHUNK), fs=Fs, nfft=2*SIZE, scaling='spectrum') # copy over all the frequency data to that column self.graph[:,self.graph_pos] = psd[:-1] # label all points greater than the threshold as marked indices = [i for i,v in enumerate(psd[:-1]) if v > self.threshold] self.graph[indices,self.graph_pos] = np.nan self.graph_pos = (self.graph_pos + 1) % SIZE # rotate data so we get a nice scrolling spectrogram unscaled_data = np.roll(self.graph,-self.graph_pos,1) # scale graph to take full example of 8 bit # display. Spectrogram floating point power values scaled to be between 0-255 minval = np.nanmin(unscaled_data) minval = 0 maxval = np.nanmax(unscaled_data) - minval scaled_data = (unscaled_data-minval)/maxval # create a bitmap of the colored spectrogram colored = np.uint8(255*matplotlib.cm.hot(scaled_data)[:,:,0:3]) # make all thresholded values blue colored[np.isnan(scaled_data)] = [0,0,255] # update the bmp self.bmp = wx.BitmapFromBuffer(SIZE, SIZE, colored) self.Refresh() return (None, pyaudio.paContinue)
def __init__(self, *args, **kwargs): wx.Frame.__init__(self, *args, **kwargs) self.page_nbr = 0 self.page_image = None self.line = None # Make Panel self.panel = wx.Panel(self, -1) self.current_text = rcs.RowColSizer() self.pageCtrl = wx.TextCtrl(self.panel, wx.ID_ANY, str(self.page_nbr), size=( 40, 20, ), style=wx.TE_READONLY) self.linesCtrl = wx.TextCtrl(self.panel, wx.ID_ANY, '', size=( WIDTH - 100, 80, ), style=wx.TE_MULTILINE | wx.TE_READONLY) self.editCtrl = wx.TextCtrl(self.panel, wx.ID_ANY, '', size=( WIDTH - 100, 40, ), style=wx.TE_PROCESS_ENTER) self.imageCtrl = wx.StaticBitmap(self.panel, wx.ID_ANY, wx.BitmapFromBuffer( 1, 1, array.array('B', ( 0, 0, 0, ))), size=( WIDTH - 100, 180, )) self.Bind(wx.EVT_TEXT_ENTER, self.OnEdit, self.editCtrl) self.current_text.Add(wx.StaticText(self.panel, wx.ID_ANY, 'Page'), row=1, col=1) self.current_text.Add(self.pageCtrl, row=1, col=2) self.current_text.Add(self.linesCtrl, row=2, col=2, rowspan=4) self.current_text.Add(self.editCtrl, row=6, col=2) self.current_text.Add(self.imageCtrl, row=7, col=2) self.last_page_callback = None self.Bind(wx.EVT_CLOSE, self.OnClose)
def onNextFrame(self, evt): frame = gui.cvQueryFrame(self.capture) if frame: cv.cvCvtColor(frame, frame, cv.CV_BGR2RGB) self.bmp = wx.BitmapFromBuffer(frame.width, frame.height, frame.imageData) self.Refresh() evt.Skip()
def draw(self, gc, state): w, h = self.size bitmap = wx.BitmapFromBuffer(w, h, self.data) m = gc.CreateMatrix() gc.SetTransform(m) gc.ConcatTransform(state['trafo']) gc.ConcatTransform(state['matrix']) gc.DrawBitmap(bitmap, 0, 0, w, h) gc.SetTransform(state['trafo'])
def GetWxBitmap(self): (width, height) = self._size if self._format == wx.BitmapBufferFormat_RGB: return wx.BitmapFromBuffer(width, height, lz4.loads(self._data)) else: return wx.BitmapFromBufferRGBA(width, height, lz4.loads(self._data))
def redraw(self, img): assert (img.ndim == 3) assert (img.dtype == np.uint8) assert (img.shape[0] == self.size[1] and img.shape[1] == self.size[0]) buf = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) bmp = wx.BitmapFromBuffer(img.shape[1], img.shape[0], buf) self.stbmp.SetBitmap(bmp)