Files
ofm/src/ofm_code_cbc_v1/new_updated_ofm .py

256 lines
7.7 KiB
Python
Raw Normal View History

2018-10-21 23:37:25 +05:30
#import matlab.engine
from PyQt5 import QtCore, QtGui, uic, QtWidgets
import sys,os
import numpy as np
import cv2
import serial,sys
import threading, time, Queue
from datetime import datetime
name=raw_input("enter patient's name ")
#age=raw_input("enter age ")
today=datetime.now().date()
today1=datetime.now().time()
#t = datetime.time(datetime.now())
path = "./%s-" %name + str(today)
try:
os.mkdir(path)
except OSError:
print ("Creation of the directory %s failed" % path)
else:
print ("Successfully created the directory %s " % path)
running = False
capture_thread = None
#for i in range(3):
form_class = uic.loadUiType("simple.ui")[0]
q = Queue.Queue()
ser = serial.Serial('COM3',57600)
capture = cv2.VideoCapture(0)
capture_duration = 60*1
q_write = Queue.Queue()
def grab(width, height):
global running,q,capture
#capture = cv2.VideoCapture(0)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, width)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
capture.set(cv2.CAP_PROP_EXPOSURE, -13)
#print (capture.get(cv2.CAP_PROP_EXPOSURE) )
while(running):
retval, img = capture.read()
if q.qsize() < 7200:
q.put(img)
class OwnImageWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(OwnImageWidget, self).__init__(parent)
self.image = None
def setImage(self, image):
self.image = image
sz = image.size()
self.setMinimumSize(sz)
self.update()
def paintEvent(self, event):
qp = QtGui.QPainter()
qp.begin(self)
if self.image:
qp.drawImage(QtCore.QPoint(0, 0), self.image)
qp.end()
class MyWindowClass(QtWidgets.QMainWindow, form_class):
location=''
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.resize(730,730)
self.currzpos=0
#btn1 = QtWidgets.QPushButton('X+', self)
#btn2 = QtWidgets.QPushButton('X-', self)
#btn1.move(200, 500)
#btn2.move(300, 500)
self.startButton.clicked.connect(self.start_clicked)
self.startButton4.clicked.connect(self.main)
self.window_width = self.ImgWidget.frameSize().width()
self.window_height = self.ImgWidget.frameSize().height()
self.ImgWidget = OwnImageWidget(self.ImgWidget)
self.startButton1.clicked.connect(self.xPos)
self.startButton2.clicked.connect(self.xNeg)
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.update_frame)
self.timer.start(.01)
self.startButton3.clicked.connect(self.fCapture)
self.startButton5.clicked.connect(self.home)
self.startButton6.clicked.connect(self.start)
self.startButton7.clicked.connect(self.xPoss)
self.startButton8.clicked.connect(self.xNegg)
def main(self):
capture_thread2 = threading.Thread(target=self.autofocus, args = ())
capture_thread2.start()
#capture_thread2.join()
#self.autofocus()
def home(self):
ser.write('Q')
self.currzpos=0
def start(self):
self.Zpulserate = 57600
#ser.write('S')
auto_travel=-650000
self.gotoZ(auto_travel)
def gotoZ(self,zValue):
zValue = int(zValue)
if self.currzpos != zValue:
zValue_str = str("%($)07d" % {"$":zValue})
print zValue_str
ser.write('M')
ser.write(str(zValue_str))
time.sleep(abs(self.currzpos-zValue)/self.Zpulserate)
self.currzpos = int(zValue)
def autofocus(self):
global q
Z_travel_for_crude = 16000; crude_step_count = 200;
crude_pulse_count = Z_travel_for_crude / crude_step_count;
crude_max_var=0; crude_loc_max_var=0;
crude_start_loc = self.currzpos -(Z_travel_for_crude/2);
self.gotoZ(crude_start_loc)
crude_curr_loc = crude_start_loc;
kernel = np.ones((5,5),np.float32)/25
for i in range(int(crude_pulse_count)):
img=q.get()
crude_curr_var = np.var(cv2.filter2D(img,-1,kernel))
#cv2.imwrite("image/image_crude_auto{0}.jpg".format(i),img)
print(crude_curr_var,'||',self.currzpos)
if crude_curr_var > crude_max_var:
crude_max_var = crude_curr_var
crude_loc_max_var = crude_curr_loc
image=img
crude_curr_loc = crude_curr_loc + crude_step_count
self.gotoZ(crude_curr_loc)
time.sleep(.075)
#a="image/image_crude%d.jpg"%crude_loc_max_var
#cv2.imwrite(a ,image)
self.gotoZ(crude_loc_max_var)
def xPos(self):
self.currzpos=int(self.currzpos)
ser.write('G')
self.currzpos = self.currzpos + 1
print self.currzpos
def xNeg(self):
self.currzpos=int(self.currzpos)
ser.write('T')
print self.currzpos
def xPoss(self):
self.currzpos=int(self.currzpos)
ser.write('D')
self.currzpos = self.currzpos + 1
print self.currzpos
def xNegg(self):
self.currzpos=int(self.currzpos)
ser.write('A')
self.currzpos = self.currzpos - 1
print self.currzpos
def start_clicked(self):
global running
running = True
capture_thread.start()
def update_frame(self):
global q
if not q.empty():
img = q.get()
img_height, img_width, img_colors = img.shape
scale_w = float(self.window_width) / float(img_width)
scale_h = float(self.window_height) / float(img_height)
scale = min([scale_w, scale_h])
if scale == 0:
scale = 1
img = cv2.resize(img, None, fx=scale, fy=scale, interpolation = cv2.INTER_CUBIC)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
height, width, bpc = img.shape
bpl = bpc * width
image = QtGui.QImage(img.data, width, height, bpl, QtGui.QImage.Format_RGB888)
self.ImgWidget.setImage(image)
def fCapture(self):
capture_thread3 = threading.Thread(target=self.fCapture_read, args = ())
capture_thread3.start()
def fCapture_read(self):
global capture,capture_duration
global q_write
t_end = time.time() + capture_duration
self.counter=0
print "frame capture started"
while(capture.isOpened()):
ret, frame = capture.read()
if time.time() < t_end:
if (ret==True):
self.counter=self.counter+1
if q_write.qsize()<120*capture_duration:
q_write.put(frame)
else:
print "frames captured"
print self.counter
self.fcapture_write()
#cap.release()
def fcapture_write(self):
global q_write,capture
i=0
if not q_write.empty():
for i in range (self.counter):
img=q_write.get()
i=i+1
cv2.imwrite(str(path)+ "/image%d.bmp" %i,img)
self.call_matlab()
def call_matlab(self):
print "calling matlab"
print path
import matlab.engine as m
eng = m.start_matlab()
eng.new_whole_blood_segment_odroid(path)
#print "calling matlab"
#mlab.new_whole_blood_segment_odroid(path)
if __name__ == '__main__' :
capture_thread = threading.Thread(target=grab, args = (640,480))
app = QtWidgets.QApplication(sys.argv)
w = MyWindowClass()
w.setWindowTitle('OFM GUI')
w.show()
app.exec_()