My project for recognizing words typed in a printed prescription and finding out the pills available in the store and show it to the user. I am using PyQt5 for this and the scripts end without an error - "Process finished with exit code -1073740791 (0xC0000409)" . I dont have an idea on what is the problem. please help me.
import cv2
import sys
import time
from PyQt5 import QtCore
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication,QMainWindow , QMessageBox
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.uic import loadUi
import os
from PIL import Image
import pytesseract
import pandas as pd
class dispensercode(QMainWindow):
def __init__(self):
self.name = 'delila'
super(dispensercode, self).__init__()
loadUi('gui1.ui',self)
self.logic = 0
self.capdone = 0
self.count = 1
self.x = 'm'
self.Confirm.clicked.connect(self.Confirmed)
self.Camon.clicked.connect(self.Camturn)
self.Capture.clicked.connect(self.captureClicked)
@pyqtSlot()
def Confirmed(self):
name = self.NameInput.toPlainText()
self.new_name = name
self.label2.setText('Patient name is ' + name)
return self.new_name
def Camturn(self):
cap = cv2.VideoCapture(0)
while (cap.isOpened()):
ret, frame = cap.read()
if ret == True: # ret is if cam is working
print('here')
self.displayimage(frame, 1) # frame is the image
cv2.waitKey()
if (self.logic == 2):
cv2.imwrite('%s.png' % (self.new_name), frame) # %s will be substituted by %(self.value)
self.capdone = 1
print('cap done is ', self.capdone)
# self.textBrowser1.setText('image saved pres cap for another image')
cap.release()
cv2.destroyAllWindows()
if (self.capdone == 1):
self.loadClicked()
self.ocr()
self.capdone = 0
#-------------------create verification pop up window-----------------------------------#
msg = QMessageBox()
msg.setWindowTitle('please verify whether the ocr is correct')
msg.setText(' Click "Accept" if true "Reject" if false')
msg.setIcon(QMessageBox.Question)
msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
msg.setInformativeText(self.text)
msg.buttonClicked.connect(self.popup_button)
x = msg.exec_()
print('this is before while is running')
if self.ver() == False:
print('this while is running')
if self.count <= 3:
self.count = self.count + 1
cap = cv2.VideoCapture(0)
while (cap.isOpened()):
ret, frame = cap.read()
if ret == True: # ret is if cam is working
print('here')
self.displayimage(frame, 1) # frame is the image
cv2.waitKey()
if (self.logic == 2):
cv2.imwrite('%s.png' % (self.new_name),
frame) # %s will be substituted by %(self.value)
self.capdone = 1
print('cap done is ', self.capdone)
# self.textBrowser1.setText('image saved pres cap for another image')
cap.release()
cv2.destroyAllWindows()
if (self.capdone == 1):
self.loadClicked()
self.ocr()
self.capdone = 0
# -------------------create verification pop up window-----------------------------------#
msg = QMessageBox()
msg.setWindowTitle('please verify whether the ocr is correct')
msg.setText(' Click "Accept" if true "Reject" if false')
msg.setIcon(QMessageBox.Question)
msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
msg.setInformativeText(self.text)
msg.buttonClicked.connect(self.popup_button)
x = msg.exec_()
elif self.count > 3:
self.label4.setText(
'please type the correct words and save the file and close the txt file and press spacebar')
os.startfile('{}.txt'.format(self.new_name))
break
else :
print('readlist is running')
self.readlist()
self.logic = 1
else:
print('return not found')
def captureClicked(self):
self.logic = 2
def displayimage(self, img, window=1):
qformat = QImage.Format_Indexed8
if len(img.shape) == 3:
if (img.shape[2]) == 4:
qformat = QImage.Format_RGBA8888
else:
qformat = QImage.Format_RGB888
img = QImage(img, img.shape[1], img.shape[0], qformat) # Qimage is used to load the image
img = img.rgbSwapped()
self.label1.setPixmap(QPixmap.fromImage(img)) # QPixmap is used to show the image
self.label1.setScaledContents(True) # scale the image according th the frame
self.label1.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
def loadClicked(self):
img1 = cv2.imread("{}.png".format(self.new_name))
self.displayimagef(img1, 1)
cv2.destroyAllWindows()
def displayimagef(self, img1, window=1):
qformat = QImage.Format_Indexed8
if len(img1.shape) == 3:
if (img1.shape[2]) == 4:
qformat = QImage.Format_RGBA8888
else:
qformat = QImage.Format_RGB888
img1 = QImage(img1, img1.shape[1], img1.shape[0], qformat) # Qimage is used to load the image
img1 = img1.rgbSwapped()
self.label1.setPixmap(QPixmap.fromImage(img1)) # QPixmap is used to show the image
self.label1.setScaledContents(True) # scale the image according th the frame
self.label1.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
def ocr(self):
# load the example image and convert it to grayscale
image = cv2.imread("{}.png".format(self.new_name))
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# write the grayscale image to disk as a temporary file so we can
# apply OCR to it
filename = "{}.png".format(os.getpid())
cv2.imwrite(filename, gray)
# load the image as a PIL/Pillow image, apply OCR, and then delete
# the temporary file
self.text = pytesseract.image_to_string(Image.open(filename))
os.remove(filename)
self.label3.setText(self.text)
print('converted to text')
# export to .txt file
# stdoutOrigin = sys.stdout
with open("{}.txt".format(self.new_name), "w") as f:
f.write(self.text)
def popup_button(self, i):
print(i.text())
if i.text() == '&Yes':
self.ver = True
print(self.ver)
return self.ver
else:
self.ver = False
print(self.ver)
return self.ver
def readlist(self):
with open("{}.txt".format(self.new_name)) as f1:
os.startfile('{}.txt'.format(self.new_name))
flat_list = [word for line in f1 for word in line.split()]
print(flat_list)
# ---------read the csv file of pills-------------------------------------------#
df = pd.read_excel('items.xlsx', sheet_name=0) # can also index sheet by name or fetch all sheets
bin_no = df['BIN no'].tolist()
Name = df['Name'].tolist()
content = df['content'].tolist()
print(bin_no)
print(Name)
print(content)
# iterating through the list
final_list = []
i = 0
for i in range(len(flat_list)):
for j in range(len(Name)):
if Name[j] == flat_list[i]:
if content[j] == flat_list[i + 1]:
final_list.append(bin_no[j])
final_list.append(Name[j])
final_list.append(content[j])
x = i
for k in range(x, len(flat_list)):
if 'Supply' == flat_list[k]:
print(flat_list[k + 1])
final_list.append(flat_list[k + 1])
else:
continue
else:
continue
else:
continue
app =QApplication(sys.argv)
window = dispensercode()
window.show()
try:
sys.exit(app.exec_())
except:
print('exiting')
In here the main error that is shown is "Process finished with exit code -1073740791 (0xC0000409)"
User contributions licensed under CC BY-SA 3.0