本项目用的Python,需要安装opencv库与numpy还有Pillow库,具体安装方法自行bing。
导入所有库:
1 2 3 4 5 6 7
| import tkinter as tk from tkinter import * from tkinter import filedialog import cv2 import requests import numpy as np from PIL import Image, ImageTk
|
项目里主要采用了Paddle OCR,实际上是采用的一个云识别的技术。
1 2 3 4 5 6 7 8 9 10 11 12 13
| def analy_img(self, event=None): if self.filenames: for k in range(len(self.filenames)): password='8907' url = "http://www.iinside.cn:7001/api_req" filePath= self.filenames[k] data={ 'password':password, 'reqmode':'ocr_pp' } files=[('image_ocr_pp',('wx.PNG',open(filePath,'rb'),'application/octet-stream'))] headers = {} response = requests.post( url, headers=headers, data=data, files=files)
|
将返回的response以text形式返回,然后进行字符串的处理分析。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| txt = response.text yi = txt.count("壹圆") wu = txt.count("伍圆") shi = txt.count("拾圆") ershi = txt.count("贰拾圆") wushi = txt.count("伍拾圆") yibai = txt.count("壹佰圆") tshi = shi-ershi-wushi num = yi+wu+tshi+ershi+wushi+yibai num = str(num) bizhi= yi*1+wu*5+tshi*10+ershi*20+wushi*50+yibai*100 bizhi = str(bizhi) over = ("共有纸币"+num+"张,共"+bizhi+"元") global flag flag.set(over)
|
选择文件:

1 2 3
| def choose_pic(self, event=None): self.filenames.clear() self.filenames += filedialog.askopenfilenames()
|
显示图片

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| def display_image(self, event=None): self.pic_filelist.clear() self.imgt_list.clear() self.image_labellist.clear()
for widget in self.image_frame.winfo_children(): widget.destroy()
for i in range(len(self.filenames)): self.pic_filelist.append(Image.open(self.filenames[i]).resize((200,200))) self.imgt_list.append(ImageTk.PhotoImage(image=self.pic_filelist[i])) self.image_labellist.append(Label(self.image_frame, highlightthickness=0, borderwidth=0)) self.image_labellist[i].configure(image=self.imgt_list[i]) self.image_labellist[i].pack(side=LEFT, expand=True)
|
GUI框架部分
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
| class DisplayImage: '''用于展示选择的图片''' def __init__(self, master): self.master = master master.title("GUI") self.image_frame = Frame(master, bd=0, height=200, width=800, bg='yellow', highlightthickness=2, highlightbackground='gray', highlightcolor='black') self.image_frame.pack() self.Text_label = Label(master, text='图像预览') self.Text_label.pack() self.Choose_image = Button(master, command=self.choose_pic, text="选择图片", width=17, default=ACTIVE, borderwidth=0) self.Choose_image.pack() self.Display_image = Button(master, command=self.display_image, text="显示图片", width=17, default=ACTIVE, borderwidth=0) self.Display_image.pack() self.Analy_image = Button(master, command=self.analy_img, text="分析1", width=17, default=ACTIVE, borderwidth=0) self.Analy_image.pack() global flag flag = StringVar() flag.set('0元') self.filenames = [] self.pic_filelist = [] self.imgt_list = [] self.image_labellist = [] self.out = Label(master,textvariable = flag) self.out.pack()
|
这部分代码实现初始化框架
最终结果效果图:✌️

前面的接口是可以换的,就是那个分析的前部分,你喜欢也可以换成其他百度,阿里这些的OCR
最终代码在github,拿走点⭐吧!币值识别