Extract text from image
import cv2
import numpy as np
import pytesseract
# Path to Tesseract executable (change this according to your installation)
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
# Load the image
image_path = r'D:\image\image3.png'
image = cv2.imread(image_path)
# Check if the image was loaded successfully
if image is None:
print("Failed to load the image:", image_path)
exit(1)
# Continue with image processing
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
sharpen_kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
sharpen = cv2.filter2D(gray, -1, sharpen_kernel)
thresh = cv2.threshold(sharpen, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=1)
result = 255 - close
# Display the processed images
cv2.imshow('sharpen', sharpen)
cv2.imshow('thresh', thresh)
cv2.imshow('close', close)
cv2.imshow('result', result)
# Perform OCR
text = pytesseract.image_to_string(gray)
# Print the extracted text
print("Extracted Text:")
print(text)
# Display the original image
cv2.imshow('Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Extract text from real time video streaming
from PyQt5 import QtCore, QtGui, QtWidgets
import cv2
class FrameGrabber(QtCore.QThread):
def __init__(self, parent=None):
super(FrameGrabber, self).__init__(parent)
signal = QtCore.pyqtSignal(QtGui.QImage)
def run(self):
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
while cap.isOpened():
success, frame = cap.read()
if success:
image = QtGui.QImage(frame, frame.shape[1], frame.shape[0], QtGui.QImage.Format_BGR888)
self.signal.emit(image)
class Ui_MainWindow(QtWidgets.QMainWindow):
def __init__(self, MainWindow):
super().__init__()
self.MainWindow = MainWindow
self.setupUi(self.MainWindow)
self.grabber = FrameGrabber()
self.grabber.signal.connect(self.updateFrame)
self.grabber.start()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(840, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.imglabel = QtWidgets.QLabel(self.centralwidget)
self.imglabel.setGeometry(QtCore.QRect(0, 0, 640, 480))
self.imglabel.setObjectName("imglabel")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(640, 0, 200, 480))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(3)
self.tableWidget.setRowCount(15)
self.tableWidget.setHorizontalHeaderLabels(['Plate', 'Registration', 'Sanc'])
self.tableWidget.horizontalHeader().setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch)
self.tableWidget.horizontalHeader().setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)
self.tableWidget.horizontalHeader().setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
@QtCore.pyqtSlot(QtGui.QImage)
def updateFrame(self, image):
self.imglabel.setPixmap(QtGui.QPixmap.fromImage(image))
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Stationnement"))
def appExec(self):
self.grabber.appExec()
from PyQt5.QtWidgets import QApplication, QMainWindow
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = Ui_MainWindow(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I am trying to extract the text from the image, that was a first try, if it work then I will apply the technique to real time video straming, but both failed, I failed to extract the text from the image, at the same time I have code the real time video streaming to extract the text from live, I only success to connect to laptop web camera only, I dont know how to write the coding for real time license plate recognition, I totally have no idea how to do the next, may somebody guide me for that