From e2f6b158bc3c0fadac2e2a14d945c0dd6667191c Mon Sep 17 00:00:00 2001 From: martindrech <martindrech@df.uba.ar> Date: Fri, 10 Aug 2018 19:01:41 -0300 Subject: [PATCH] webcams and webcamROI added. Still this last needs to be integrated to the GUI, but it works on its own. --- myToolbar.py | 20 ++++++++-- myWidgets.py | 5 ++- webcamROI.py | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++ webcams.py | 32 +++++++++++++++ 4 files changed, 163 insertions(+), 5 deletions(-) create mode 100644 webcamROI.py create mode 100644 webcams.py diff --git a/myToolbar.py b/myToolbar.py index d192cf1..46ef696 100644 --- a/myToolbar.py +++ b/myToolbar.py @@ -82,11 +82,19 @@ def startWebcamsThread(): def putWebcams(checked): if checked: - t = Thread(target=startWebcamsThread, args=()) - t.start() + t_webcams = Thread(target=startWebcamsThread, args=()) + t_webcams.start() else: pass - + +def openRoi(): + t_roi = Thread(target=startRoiThread, args=()) + t_roi.start() + +def startRoiThread(): + exec(open('C:\\Users\\Usuario\\Documents\\control_app\\webcamROI.py').read()) + + def incorporate_toolbar(win, analog_control_signals): menubar = win.menuBar() @@ -113,9 +121,13 @@ def incorporate_toolbar(win, analog_control_signals): viewWebcams.setChecked(False) viewMenu.addAction(viewWebcams) + roiMenu = menubar.addMenu('Roi') + plotRoi = QAction('Open roi plot', win) + roiMenu.addAction(plotRoi) saveAct.triggered.connect(lambda: save(analog_control_signals)) loadAct.triggered.connect(lambda: load(analog_control_signals)) saveAsAct.triggered.connect(lambda: save_as(analog_control_signals, win)) openFromAct.triggered.connect(lambda: open_from(analog_control_signals, win)) - viewWebcams.toggled.connect(lambda: putWebcams(viewWebcams.isChecked()) ) \ No newline at end of file + viewWebcams.toggled.connect(lambda: putWebcams(viewWebcams.isChecked()) ) + plotRoi.toggled.connect(openRoi) \ No newline at end of file diff --git a/myWidgets.py b/myWidgets.py index 417da2a..c9d74de 100644 --- a/myWidgets.py +++ b/myWidgets.py @@ -123,6 +123,7 @@ class anal_control_signal(QWidget): self.scan_button.setStyleSheet('background-color: None') self.PbarTimer.stop() + self.sb.setEnabled(True) self.sb.setValue(self.scan_step) @@ -135,7 +136,9 @@ class anal_control_signal(QWidget): self.scan_direction = -abs(b-a)/n # this is the size in voltage of each step self.scan_step = float(self.sb.val) # from here it beggins - + + self.sb.setEnabled(False) + if self.scan_step > b or self.scan_step < a: print('scan can not start outside scan range and scan start should be lower than scan stop') else: diff --git a/webcamROI.py b/webcamROI.py new file mode 100644 index 0000000..04aa42c --- /dev/null +++ b/webcamROI.py @@ -0,0 +1,111 @@ +# -*- coding: utf-8 -*- +""" +Created on Fri Aug 10 14:21:33 2018 + +@author: Usuario +""" + + +from pyqtgraph.Qt import QtCore, QtGui +import numpy as np +import pyqtgraph as pg +import cv2 +from PyQt5.QtCore import QTimer + +cap = cv2.VideoCapture(1) + +n = int(1e2) +data = np.zeros(n) +i = 0 + +def capture(): + ret, frame = cap.read() + #frame = cv2.resize(frame, None, fx = 0.5, fy = 0.5, interpolation = cv2.INTER_CUBIC) + gray_image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + return gray_image + +def updateImage(): + im1.setImage(capture(), cmap = 'Gray') + updateRoi(lastRoi) + +## create GUI +app = QtGui.QApplication([]) +w = pg.GraphicsWindow(size=(800,800), border=True) +v = w.addViewBox(colspan=2) +v.invertY(True) ## Images usually have their Y-axis pointing downward +v.setAspectLocked(True) +im1 = pg.ImageItem(capture()) +v.addItem(im1) + + + + +imgTimer = QTimer() +imgTimer.timeout.connect(updateImage) +imgTimer.start(10) + +im2 = pg.ImageItem() +v2 = w.addViewBox(1,0) +v2.addItem(im2) + + +p = w.addPlot(row = 3, col = 0, title="Updating plot") +p.setRange(QtCore.QRectF(0, -10, 5000, 20)) +p.setAutoPan(y=True) +p.setRange(xRange = (0, n), yRange = (0, 255)) + +curve = p.plot(pen='y') + + +lastRoi = None + +def updateRoi(roi): + global im1, im2, lastRoi, data, i, curve + if roi is None: + return + lastRoi = roi + roiSlice = roi.getArrayRegion(im1.image, img=im1) + im2.setImage(roiSlice) + newData = np.mean(roiSlice) + data[i] = newData + i = np.mod(i + 1, len(data)) + curve.setData(data) + app.processEvents() ## force complete redraw for every plot + + +def updateRoiPlot(data): + global curve + curve.setData(data) + app.processEvents() ## force complete redraw for every plot + +rois = [] +#rois.append(pg.EllipseROI([110, 10], [30, 20], pen=(3,9))) +rois.append(pg.TestROI([0, 0], [20, 20], pen=(0,9))) + + +## Add each ROI to the scene and link its data to a plot curve with the same color +for r in rois: + v.addItem(r) + #c = pi1.plot(pen=r.pen) + #r.curve = c + r.sigRegionChanged.connect(updateRoi) + +def updateImage(): + global im1, arr, lastRoi + r = abs(np.random.normal(loc=0, scale=(arr.max()-arr.min())*0.1, size=arr.shape)) + im1.updateImage(arr + r) + updateRoi(lastRoi) + #updateRoiPlot(data) + +if cv2.waitKey(1) & 0xFF == ord('q'): + cap.release() + cv2.destroyAllWindows() + +w.show() + + +## Start Qt event loop unless running in interactive mode or using pyside. +if __name__ == '__main__': + import sys + if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): + QtGui.QApplication.instance().exec_() diff --git a/webcams.py b/webcams.py new file mode 100644 index 0000000..bbd9ba4 --- /dev/null +++ b/webcams.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +""" +Spyder Editor + +This is a temporary script file. +""" + +import cv2 + +cap = cv2.VideoCapture(0) +cap1 = cv2.VideoCapture(1) + +while(True): + # Capture frame-by-frame + ret, frame = cap.read() + ret1, frame1 = cap1.read() + # Our operations on the frame come here + gray = cv2.cvtColor(frame, 0) + gray1 = cv2.cvtColor(frame1, 0) + # Display the resulting frame + frame = cv2.resize(frame, None, fx = 0.5, fy = 0.5, interpolation = cv2.INTER_CUBIC) + frame1 = cv2.resize(frame1, None, fx = 0.5, fy = 0.5, interpolation = cv2.INTER_CUBIC) + final_frame = cv2.vconcat((frame, frame1)) + cv2.imshow('final_frame',final_frame) + #cv2.imshow('frame1',gray1) + + if cv2.waitKey(1) & 0xFF == ord('q'): + break + cap.release() + cv2.destroyAllWindows() + +# When everything done, release the capture -- 2.18.1