<file_sep>import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
import math


# 计算灰度直方图
def calcGrayHist(I):
    h, w = I.shape[:2]
    grayHist = np.zeros([256], np.uint64)
    for i in range(h):
        for j in range(w):
            grayHist[I[i][j]] += 1
    return grayHist
def equalHist(img):
    # 灰度图像矩阵的高、宽
    h, w = img.shape
    # 第一步：计算灰度直方图
    grayHist = calcGrayHist(img)
    # 第二步：计算累加灰度直方图
    zeroCumuMoment = np.zeros([256], np.uint32)
    for p in range(256):
        if p == 0:
            zeroCumuMoment[p] = grayHist[0]
        else:
            zeroCumuMoment[p] = zeroCumuMoment[p - 1] + grayHist[p]
    # 第三步：根据累加灰度直方图得到输入灰度级和输出灰度级之间的映射关系
    outPut_q = np.zeros([256], np.uint8)
    cofficient = 256.0 / (h * w)
    for p in range(256):
        q = cofficient * float(zeroCumuMoment[p]) - 1
        if q >= 0:
            outPut_q[p] = math.floor(q)
        else:
            outPut_q[p] = 0
    # 第四步：得到直方图均衡化后的图像
    equalHistImage = np.zeros(img.shape, np.uint8)
    for i in range(h):
        for j in range(w):
            equalHistImage[i][j] = outPut_q[img[i][j]]
    return equalHistImage



# 使用自己写的函数实现
# grayHist(img, equa)
# 使用OpenCV提供的直方图均衡化函数实现
# equa = cv.equalizeHist(img)
if __name__ == '__main__':
    equalHist('image/2.jpg')

<file_sep>import sys
import os
import numpy as np
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QFont,QPalette
from location_and_ocr_function import *
from crop_image_use_mouse import *
from qr_code import *

class MainForm(QWidget):
    def __init__(self, name = 'MainForm'):
        super(MainForm,self).__init__()
        self.setWindowTitle(name)
        self.cwd = os.getcwd() # 获取当前程序文件位置
        self.resize(500,800)   # 设置窗体大小
        self.move(50,100)
        # btn_choose
        self.btn_chooseFile = QPushButton(self)
        self.btn_chooseFile.setObjectName("btn_chooseFile")
        self.btn_chooseFile.setFont(QFont("Microsoft YaHei",10))
        self.btn_chooseFile.setText("选取文件")
        self.btn_chooseFile.move(30,30)
        self.btn_chooseFile.resize(100,30)
        self.btn_chooseFile.clicked.connect(self.slot_btn_chooseFile)
        # label_file
        self.lable_file = QLabel(self)
        self.lable_file.resize(300,30)
        self.lable_file.move(150,30)
        self.lable_file.setFont(QFont("Times new Roman",12))
        # btn_crop
        self.btn_crop = QPushButton(self)
        self.btn_crop.setObjectName("btn_crop")
        self.btn_crop.setFont(QFont("Microsoft YaHei",10))
        self.btn_crop.setText("截取图片")
        self.btn_crop.move(30,80)
        self.btn_crop.resize(100,30)
        self.btn_crop.clicked.connect(self.crop)
        # btn_detect
        self.btn_detect = QPushButton(self)
        self.btn_detect.setObjectName("btn_detect")
        self.btn_detect.setFont(QFont("Microsoft YaHei",12,))
        self.btn_detect.setText("开始检测")
        self.btn_detect.move(200,130)
        self.btn_detect.resize(100,50)
        self.btn_detect.clicked.connect(self.detect)
        # btn_auto_detect
        self.btn_auto_detect = QPushButton(self)
        self.btn_auto_detect.setObjectName("btn_detect")
        self.btn_auto_detect.setFont(QFont("Microsoft YaHei",12,))
        self.btn_auto_detect.setText("自动检测")
        self.btn_auto_detect.move(250,450)
        self.btn_auto_detect.resize(100,50)
        self.btn_auto_detect.clicked.connect(self.auto_detect)
        # btn_qrcode
        self.btn_qrcode = QPushButton(self)
        self.btn_qrcode.setObjectName("btn_qrcode")
        self.btn_qrcode.setFont(QFont("Microsoft YaHei",12,))
        self.btn_qrcode.setText("二维码识别")
        self.btn_qrcode.move(130,450)
        self.btn_qrcode.resize(100,50)
        self.btn_qrcode.clicked.connect(self.qrcode)
        # btn_image_nor
        self.btn_chooseFile = QPushButton(self)
        self.btn_chooseFile.setObjectName("btn_image_nor")
        self.btn_chooseFile.setFont(QFont("Microsoft YaHei",10))
        self.btn_chooseFile.setText("图像增强")
        self.btn_chooseFile.move(30,140)
        self.btn_chooseFile.resize(100,30)
        self.btn_chooseFile.clicked.connect(self.image_nor)
        # label_result
        self.lable_result = QLabel(self)
        self.lable_result.resize(200,30)
        self.lable_result.move(30,250)
        self.lable_result.setFont(QFont("Microsoft YaHei",12))
        self.lable_result.setText('手动识别结果：')
        # label_result
        self.lable_result = QLabel(self)
        self.lable_result.resize(200,30)
        self.lable_result.move(30,550)
        self.lable_result.setFont(QFont("Microsoft YaHei",12))
        self.lable_result.setText('自动识别结果：')
        # label_text
        self.lable_text = QLabel(self)
        self.lable_text.setWordWrap(True)
        self.lable_text.resize(300,160)
        self.lable_text.move(150,220)
        self.lable_text.setFont(QFont("Microsoft YaHei",12))
        # label_text_auto
        self.lable_text_auto = QLabel(self)
        self.lable_text_auto.setWordWrap(True)
        self.lable_text_auto.resize(300,160)
        self.lable_text_auto.move(150,550)
        self.lable_text_auto.setFont(QFont("Microsoft YaHei",12))
        # label_text_qrcode
        self.label_text_qrcode = QLabel(self)
        self.label_text_qrcode.setWordWrap(True)
        self.label_text_qrcode.resize(300, 120)
        self.label_text_qrcode.move(30, 650)
        self.label_text_qrcode.setFont(QFont("Microsoft YaHei", 12))
    def slot_btn_chooseFile(self):
        self.fileName_choose, filetype = QFileDialog.getOpenFileName(self,
                                    "选取文件",
                                    self.cwd, # 起始路径
                                    "All Files (*);;Text Files (*.txt)")   # 设置文件扩展名过滤,用双分号间隔
        self.lable_file.setText(self.fileName_choose)
    def crop(self):
        crop_image(self.lable_file.text())
    def detect(self):
        self.text = text_detect('croped.jpg')
        print(self.text)
        self.lable_text.setText(self.text)
    def auto_detect(self):
        print(self.lable_file.text())
        image = cv2.imread(self.lable_file.text())
        height= image.shape[0]
        width = image.shape[1]
        croped_image = image[int(0.83*height):height,0:int(0.75*width)] #裁剪固定位置的图片
        cv2.imwrite("croped.jpg",croped_image)
        img = cv2.imread('croped.jpg', 0)
        out = np.zeros(img.shape, np.uint8)
        cv2.normalize(img, out, 255, 0, cv2.NORM_MINMAX, cv2.CV_8U)#图像归一化增强
        cv2.imwrite("croped.jpg", out)
        cv2.imshow("crop",out)
        self.text = text_detect('croped.jpg')
        print(self.text)
        self.lable_text_auto.setText(self.text)
    def image_nor(self):
        img = cv2.imread('croped.jpg', 0)
        out = np.zeros(img.shape, np.uint8)
        cv2.normalize(img, out, 255, 0, cv2.NORM_MINMAX, cv2.CV_8U)
        cv2.imshow("croped_image",out)
        cv2.moveWindow("croped_image", 550, 100)
        cv2.imwrite("croped.jpg",out)
    def qrcode(self):
        qrcode_detect(self.lable_file.text())
        self.qrcode_text = qrcode_recognitation()
        self.label_text_qrcode.setText('二维码识别结果：'+self.qrcode_text)
if __name__=="__main__":
    app = QApplication(sys.argv)
    mainForm = MainForm('文字识别系统')
    mainForm.show()
    sys.exit(app.exec_())
<file_sep>import cv2
from aip import AipOcr

""" 你的 APPID AK SK  """
APP_ID = '15721684'
API_KEY = '<KEY>'
SECRET_KEY = '<KEY>'

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

# fname = 'image/1.jpg'

""" 读取图片 """
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

image = get_file_content(fname)

""" 调用通用文字识别, 图片参数为本地图片 """
results = client.basicAccurate(image)["words_result"]  # 直接得到字典对应所需字段
print(results)
img = cv2.imread(fname)
for result in results:
    text = result["words"]
    print(text)

# cv2.imwrite(fname[:-4]+"_result.jpg", img)C:\Users\qsp\Desktop\text_recognition\crop_image_use_mouse.py
<file_sep># text_recognition
##python开发的快递面单文本识别/n
##自动截取面单区域进行光学符号的识别
##自动定位检测二维码算法：



##制作了简易 UI界面
<file_sep>from aip import AipOcr

import numpy
import cv2

from PIL import Image, ImageDraw, ImageFont
import os

def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()
def put_chinese(image,string,position):
    img_PIL = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
    font = ImageFont.truetype('MSYH.TTC', 25)
    fillColor = (0, 255, 255)
    draw = ImageDraw.Draw(img_PIL)
    draw.text(position, string, font=font, fill=fillColor)
    img_OpenCV = cv2.cvtColor(numpy.asarray(img_PIL), cv2.COLOR_RGB2BGR)
    return img_OpenCV

def text_detect(image_path):
    APP_ID = '15734175'
    API_KEY = '<KEY>'
    SECRET_KEY = '<KEY>'
    client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
    image_path = get_file_content(image_path)
    result = client.basicAccurate(image_path)#["words_result"]
    if result.__contains__('words_result') and result['words_result_num'] != 0:
        results = result["words_result"]
        text_result = ''
        for result in results:
            text = str(result['words'])
            text_result = text_result+text+' '
        print(text_result)
        #cv2.rectangle(image, (x1 - 5, y1 - 5), (x4 + 5, y4 + 5), (0, 255, 0), 2)
    return text_result
if __name__ =='__main__':
    text_detect("croped.jpg")