栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

kivy手写数字识别app

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

kivy手写数字识别app

使用python kivy+opencv实现手写数字识别的安卓app。
app下载:
https://pan.baidu.com/s/1l1iqMYmZeHuq-c_mg15F0Q
提取码:lvev

如有问题,欢迎下方评论!

kivy安卓app实现数字识别

代码部分:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics.texture import Texture
from kivy.uix.camera import Camera
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.clock import Clock
from kivy.graphics import Color,Rectangle
import numpy as np
import cv2
from android.permissions import request_permissions, Permission

request_permissions([
    Permission.CAMERA,
    Permission.WRITE_EXTERNAL_STORAGE,
    Permission.READ_EXTERNAL_STORAGE
])


net = cv2.dnn.readNetFromTensorflow('model.pb')
def detect(src,net):

    try:
        src_copy=src.copy()
        gray=cv2.cvtColor(src,cv2.COLOR_RGB2GRAY)
        # thred=np.where(gray>140,255,0).astype('uint8')
        _,thred=cv2.threshold(gray,100,255,cv2.THRESH_BINARY_INV)
        # thred=255-thred
        #开闭运算
        k = np.ones((5, 5), np.uint8)
        #thred=cv.dilate(thred,k)
        thred = cv2.morphologyEx(thred, cv2.MORPH_CLOSE, k)
        thred=cv2.dilate(thred,k)
        # cv2.imshow('a123', thred)
        # cv2.waitKey(0)

        cnts=cv2.findContours(thred,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[0]
        # print('轮廓数=',len(cnts))
        if len(cnts)>100:
            return src_copy,thred
        nam=[]
        for i in cnts:
            area = cv2.contourArea(i)
            if area<50:
                continue
            # if aa!=1:
            #     b=20
            # else:b=15
            # print('面积=',area)
            b=50
            if area>=b:
                x, y, w, h = cv2.boundingRect(i)

                x2=x-10
                y2=y-10
                w2=w+20
                h2=h+20
                cv2.rectangle(src_copy,(x2,y2),(x2+w2,y2+h2),(0,255,0),2)
                lkuo=thred[y:y+h,x:x+w]

                da = max(h, w)
                rate = da / 40
                ro = cv2.resize(lkuo, (int(w / rate), int(h / rate)))

                h, w = ro.shape
                t, b = int((43 - h) / 2), 43 - h - int((43 - h) / 2)
                l, r = int((43 - w) / 2), 43 - w - int((43 - w) / 2)

                ro = cv2.copyMakeBorder(ro, t, b, l, r, cv2.BORDER_CONSTANT, value=0)

                ro = cv2.resize(ro, (40, 40))
                ro = np.where(ro > 0, 255, 0).astype('float32')

                # cv.imshow('a123', ro)
                # cv.waitKey(0)

                ro = ro / 255
                # ro=np.reshape(ro,(1,40,40,1))

                blob=ro.reshape((1,1,40,40))
                net.setInput(blob)

                out=net.forward()
                #图上标出数字
                number=np.argmax(out,axis=-1)
                nam.append((number[0],x2, y2-5,h/40))
                #cv.putText(src_copy, str(number[0]), (x2, y2-5), cv.FONT_HERSHEY_SIMPLEX, h/40, (0, 0, 255), 2)

                #print('opencv预测结果:',np.argmax(out,axis=-1),'tensorflow预测结果:',np.argmax(out2,axis=-1))
        if len(nam)==0:
            return src_copy,thred

        for ii in nam:
            cv2.putText(src_copy, str(ii[0]), (ii[1], ii[2]), cv2.FONT_HERSHEY_SIMPLEX, ii[3], (0, 0, 255), 2)

        return src_copy,thred
    except:
        return src,src

class MyLayout(BoxLayout):
    def __init__(self):
        super(MyLayout, self).__init__()
        self.orientation='vertical'
        self.image1 = Image()
        self.image1.color=1,1,1,0
        self.image1.allow_stretch=True

        self.btn=Button(text='OPEN CAMERA')

        with self.canvas:
            Color(1,1,1,1)
            self.rect=Rectangle(pos=self.pos,size=self.size,source='11.jpg')
            self.bind(pos=self.update,size=self.update)

        self.padding=[0,0,0,10]
        self.spacing=10
        self.btn.size_hint=.3,.06
        self.btn.pos_hint={'center_x':0.5,'center_y':.5}
        self.btn.background_color=1,1,1,.8
        # self.btn.size_hint_y=.1
        # self.btn.size_hint_x=.2
        self.btn.bind(on_press=self.press_btn)

        self.camera = Camera(resolution=(640, 480))
        self.camera.play=False

        self.add_widget(self.image1)
        self.add_widget(self.btn)
        Clock.schedule_interval(self.pr, 0)
    def update(self,*args):
        self.rect.pos=self.pos
        self.rect.size=self.size
    def press_btn(self,arg):
        self.camera.play=not self.camera.play
        if self.camera.play:
            self.image1.color=1,1,1,1
            self.btn.text='CLOSE CAMERA'
        else:
            self.image1.color = 1, 1, 1, 0
            self.btn.text = 'OPEN CAMERA'
    def pr(self, nap):
        if self.camera.texture==None:

            return
        img_rgb=self.texture_to_numpy(self.camera.texture)[:,:,:3]

        img_rgb_90=np.rot90(img_rgb,-1)
        img_rgb_zy=img_rgb_90[:,::-1,:]

        out,_=detect(img_rgb_zy,net)
        #cv2.putText(img_rgba_copy, str(self.counter), (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 3)#

        texture1=self.numpy_to_texture(out)
        self.image1.texture=texture1



    # 将Texture格式的图片对象转换为numpy
    def texture_to_numpy(self, data):
        image = np.asarray(bytearray(data.pixels), dtype='uint8').reshape((data.height, data.width, 4))
        # r_chanel = np.copy(image[:, :, 0])
        # g_chanel = np.copy(image[:, :, 1])
        # b_chanel = np.copy(image[:, :, 2])
        # image[:, :, 0] = b_chanel
        # image[:, :, 1] = g_chanel
        # image[:, :, 2] = r_chanel
        #返回rgba图像
        return image

    # 将numpy格式图片对象转为Texture
    def numpy_to_texture(self, frame):
        # frame=cv2.imread("ddd.jpg")
        buf1 = cv2.flip(frame, 0)
        buf = buf1.tostring()
        image_texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='rgb')
        image_texture.blit_buffer(buf, colorfmt='rgb', bufferfmt='ubyte')
        return image_texture


class MyApp(App):
    def build(self):



        return MyLayout()


if __name__ == '__main__':
    MyApp().run()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/689091.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号