python+pyqt5 文字识别+边缘检测并保存图像

# -*- coding: utf-8 -*-
import string

import Image
from PyQt5 import QtWidgets, QtGui
import pytesseract
from image import Ui_Form
import sys
import cv2
from time import sleep
import numpy as np

class Widget(QtWidgets.QWidget,Ui_Form): #主窗口
    def __init__(self):
        super(Widget, self).__init__()
        self.setupUi(self)#初始化主窗口类下的setupUi 函数
    def open(self): #注意,打开的路径不能有中文
        #imgName, imgType = QFileDialog.getOpenFileName(self, "打开图片", "", "*.jpg;;*.png;;All Files(*)")
        self.imgName, self.filename = QtWidgets.QFileDialog.getOpenFileName(None, "打开图片", "", "default(*.png;*jpg;*bmp);;ALL Files(*)")
        jpg = QtGui.QPixmap(self.imgName).scaled(self.label.width(), self.label.height())
        self.label.setPixmap(jpg)
        print(self.imgName)
    def transition(self):
        self.Image=Image.open(self.imgName)  # 打开图片
        #Image = Image.open('1.png')   # 打开图片
       # self.progressBar.setProperty("value", 0)
        text = pytesseract.image_to_string(self.Image, lang='chi_sim')  #使用简体中文解析图片
        self.textBrowser.append(text)
        QtWidgets.QApplication.processEvents()#刷新,防止界面出现假死现象
        #self.progressBar.setProperty("value", 100)
        print(text)
    def opencvA(self):
        #img=cv2.imread('cloud.jpg')
        print(self.imgName)
        self.img = cv2.imread(self.imgName)
        # 灰度图像
        gray=cv2.cvtColor(self.img, cv2.COLOR_BGR2GRAY)
        # 二值化
        ret, binary=cv2.threshold(gray, 170, 250, cv2.THRESH_BINARY)
        contours, hierarchy=cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#       # 以圆形框出云朵
        # for i in range(len(contours)):
        #   (x, y), radius = cv2.minEnclosingCircle(contours[i])
        #   center = (int(x), int(y))
        #   radius = int(radius)
        #   img = cv2.circle(img, center, radius, (0, 255, 0), 2)
        # 以云朵边界轮廓框出云朵
        cv2.drawContours(self.img, contours, -1, (0, 0, 255), 3)
        cv2.imshow("img", self.img)
        cv2.waitKey(0)
    def savepic(self):
        filename=QtWidgets.QFileDialog.getSaveFileName(None, "保存文件", ".","Image Files(*.jpg *.png)",)
        cv2.imwrite(filename[0],self.img, [int(cv2.IMWRITE_JPEG_QUALITY), 70]) #注意保存的路径不能有中文
        print(filename[0])

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    my_pyqt_form = Widget() #子窗口定义
    my_pyqt_form.show()# 主窗口显示
    sys.exit(app.exec_())

你可能感兴趣的:(python+pyqt5 文字识别+边缘检测并保存图像)