-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocr_text.py
111 lines (89 loc) · 3.48 KB
/
ocr_text.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import sys
from os import path
import cv2
import numpy as np
from PyQt5 import QtCore
from PyQt5 import QtWidgets
from PyQt5 import QtGui
import pytesseract
from PIL import Image
from pytesseract import image_to_string
from gtts import gTTS
import os
pytesseract.pytesseract.tesseract_cmd = "C:/Program Files/Tesseract-OCR/tesseract.exe"
tessdata_dir_config = '--tessdata-dir "C:/Program Files/Tesseract-OCR/tessdata"'
class RecordVideo(QtCore.QObject):
image_data = QtCore.pyqtSignal(np.ndarray)
def __init__(self, camera_port=0, parent=None):
super().__init__(parent)
self.camera = cv2.VideoCapture(camera_port)
self.timer = QtCore.QBasicTimer()
def start_recording(self):
self.timer.start(0, self)
def timerEvent(self, event):
if (event.timerId() != self.timer.timerId()):
return
read, data = self.camera.read()
if read:
self.image_data.emit(data)
def framesave(self):
read, data = self.camera.read()
if read:
cv2.imwrite('scanslip.png',data)
img=Image.fromarray(data)
img.load()
text=pytesseract.image_to_string(img, lang='eng', config=tessdata_dir_config)
print ('Text_Found: ',text,len(text))
class FaceDetectionWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.image = QtGui.QImage()
self._red = (0, 0, 255)
self._width = 2
self._min_size = (30, 30)
def image_data_slot(self, image_data):
self.image = self.get_qimage(image_data)
if self.image.size() != self.size():
self.setFixedSize(self.image.size())
self.update()
def get_qimage(self, image: np.ndarray):
height, width, colors = image.shape
bytesPerLine = 3 * width
QImage = QtGui.QImage
image = QImage(image.data,
width,
height,
bytesPerLine,
QImage.Format_RGB888)
image = image.rgbSwapped()
return image
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.drawImage(0, 0, self.image)
self.image = QtGui.QImage()
class MainWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.face_detection_widget = FaceDetectionWidget()
# TODO: set video port
self.record_video = RecordVideo()
image_data_slot = self.face_detection_widget.image_data_slot
self.record_video.image_data.connect(image_data_slot)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.face_detection_widget)
self.run_button = QtWidgets.QPushButton('Start')
layout.addWidget(self.run_button)
self.run_button.clicked.connect(self.record_video.start_recording)
self.screenshot = QtWidgets.QPushButton('Snap Shot')
layout.addWidget(self.screenshot)
self.screenshot.clicked.connect(self.record_video.framesave)
self.setLayout(layout)
def main():
app = QtWidgets.QApplication(sys.argv)
main_window = QtWidgets.QMainWindow()
main_widget = MainWidget()
main_window.setCentralWidget(main_widget)
main_window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()