本项目用的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)):#这里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 #十元与二十、五十均带有十,用tshi代表真正的十个数
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作为全局参数,这样才能容易把flag传出去显示到GUI上。
flag.set(over) #将over得到的值设置给flag,等会flag要显示到GUI上

选择文件:

选择图片.png

1
2
3
def choose_pic(self, event=None):
self.filenames.clear()
self.filenames += filedialog.askopenfilenames()

显示图片

显示图片.png

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
flag.set('0元')
self.filenames = []
self.pic_filelist = []
self.imgt_list = []
self.image_labellist = []
#显示结果模块
self.out = Label(master,textvariable = flag)#用可变text显示flag
self.out.pack()

这部分代码实现初始化框架

最终结果效果图:✌️

分析结果.png

前面的接口是可以换的,就是那个分析的前部分,你喜欢也可以换成其他百度,阿里这些的OCR

最终代码在github,拿走点⭐吧!币值识别