{"id":17798,"date":"2023-12-20T11:06:33","date_gmt":"2023-12-20T03:06:33","guid":{"rendered":"https:\/\/www.zhidianwl.com\/zhidian\/?p=17798"},"modified":"2023-12-20T11:06:33","modified_gmt":"2023-12-20T03:06:33","slug":"python%e5%bc%80%e5%8f%91%e5%ae%89%e5%8d%93app%e7%ad%94%e9%a2%98%e5%8d%a1%e8%af%86%e5%88%ab%e6%80%8e%e4%b9%88%e5%81%9a%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.com\/zhidian\/2023\/17798\/","title":{"rendered":"python\u5f00\u53d1\u5b89\u5353app\u7b54\u9898\u5361\u8bc6\u522b\u600e\u4e48\u505a\uff1f"},"content":{"rendered":"

\u5728\u672c\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u5982\u4f55\u4f7f\u7528Python\u5f00\u53d1\u4e00\u4e2a\u5b89\u5353\u5e94\u7528\uff0c\u8be5\u5e94\u7528\u53ef\u4ee5\u8bc6\u522b\u7b54\u9898\u5361\u4e0a\u7684\u7b54\u6848\u3002\u6211\u4eec\u5c06\u91c7\u7528\u4e00\u4e2a\u540d\u4e3aKivy\u7684Python\u6a21\u5757\u6765\u5b9e\u73b0\u5b89\u5353App\u7684\u5f00\u53d1\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Kivy\u548c\u5176\u4ed6\u5fc5\u8981\u7684\u5e93\uff0c\u7136\u540e\u5b9e\u73b0\u4e00\u79cd\u65b9\u6cd5\u6765\u8bc6\u522b\u7b54\u9898\u5361\u4e0a\u7684\u7b54\u6848\u3002<\/p>\n

### \u4e00\u3001\u73af\u5883\u914d\u7f6e<\/p>\n

1. \u5b89\u88c5Python<\/p>\n

2. \u5b89\u88c5Kivy\uff1a`pip install kivy`<\/p>\n

3. \u5b89\u88c5OpenCV\uff1a`pip install opencv-python`<\/p>\n

4. \u5b89\u88c5NumPy: `pip install numpy`<\/p>\n

5. \u5b89\u88c5Pytesseract: `pip\u5b89\u5353app\u5236\u4f5c<\/a> install pytesseract`<\/p>\n

### \u4e8c\u3001\u521b\u5efaKivy\u5e94\u7528<\/p>\n

1. \u65b0\u5efa\u4e00\u4e2a\u540d\u4e3a`answer_recognizer.py`\u7684\u6587\u4ef6<\/p>\n

2. \u5728\u6587\u4ef6\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2aKivy\u5e94\u7528\u7684\u57fa\u672c\u6846\u67b6\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

“`python<\/p>\n

import kivy<\/p>\n

from kivy.app import App<\/p>\n

from kivy.uix.boxlayout import BoxLayout<\/p>\n

class MyApp(App):<\/p>\n

def build(self):<\/p>\n

layout = BoxLayout()<\/p>\n

return layout<\/p>\n

if __name__ == ‘__main__’:<\/p>\n

MyApp().run()<\/p>\n

“`<\/p>\n

### \u4e09\u3001\u7b54\u9898\u5361\u8bc6\u522b<\/p>\n

1. \u5bfc\u5165\u6240\u9700\u7684\u5e93\uff1a<\/p>\n

“`python<\/p>\n

import cv2<\/p>\n

import numpy as np<\/p>\n

import pytesseract<\/p>\n

“`<\/p>\n

2. \u5b9a\u4e49\u51fd\u6570`process_image`\u7528\u4e8e\u52a0\u8f7d\u5e76\u5904\u7406\u56fe\u7247\uff1a<\/p>\n

“`python<\/p>\n

def process_image(image_path):<\/p>\n

image = cv2.imread(image_path)<\/p>\n

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n

thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)<\/p>\n

return thresh<\/p>\n

“`<\/p>\n

3. \u5b9a\u4e49\u51fd\u6570`extract_answers`\u7528\u4e8e\u63d0\u53d6\u7b54\u9898\u5361\u4e0a\u7684\u7b54\u6848\uff1a<\/p>\n

“`python<\/p>\n

def extract_answers(processed_image):<\/p>\n

pytesseract.pytesseract.tesseract_cmd = r’\/path\/to\/tesseract’<\/p>\n

config = “-c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ –psm 6”<\/p>\n

answers = pytesseract.image_to_string(processed_image, config=config, lang=’eng’)<\/p>\n<\/p>\n

return answers<\/p>\n

“`<\/p>\n

### \u56db\u3001\u96c6\u6210\u7b54\u9898\u5361\u8bc6\u522b\u5230Kivy\u5e94\u7528<\/p>\n

1. \u5728Kivy\u5e94\u7528\u7684\u754c\u9762\u4e0a\u6dfb\u52a0\u4e00\u4e2a\u52a0\u8f7d\u56fe\u7247\u7684\u6309\u94ae\u548c\u4e00\u4e2a\u7528\u4e8e\u663e\u793a\u8bc6\u522b\u7ed3\u679c\u7684\u6587\u672c\u6846\uff1a<\/p>\n

“`python<\/p>\n

from kivy.uix.button import Button<\/p>\n

from kivy.uix.textinput import TextInput<\/p>\n

from kivy.uix.popup import Popup<\/p>\n

from kivy.uix.filechooser import FileChooserIconView<\/p>\n

class MyApp(App):<\/p>\n

def build(self):<\/p>\n

layout = BoxLayout(orientation=’vertical’)<\/p>\n

self.file_chooser = FileChooserIconView()<\/p>\n

layout.add_widget(self.file_chooser)<\/p>\n

load_button = Button(text=”\u52a0\u8f7d\u7b54\u9898\u5361\u56fe\u7247”, on_release=self.load_image)<\/p>\n

layout.add_widget(load_button)<\/p>\n

self.result_textinput = TextInput(hint_text=”\u8fd9\u91cc\u663e\u793a\u7b54\u6848”)<\/p>\n

layout.add_widget(self.res\u5b89\u5353app<\/a>ult_textinput)<\/p>\n

return layout<\/p>\n

“`<\/p>\n

2. \u5b9e\u73b0\u52a0\u8f7d\u56fe\u7247\u548c\u663e\u793a\u8bc6\u522b\u7ed3\u679c\u7684\u529f\u80fd\uff1a<\/p>\n

“`python<\/p>\n

def load_image(self, instance):<\/p>\n

try:<\/p>\n

selected_image_path = self.file_chooser.selection[0]<\/p>\n

processed_image = process_image(selected_image_path)<\/p>\n

extracted_answers = extract_answers(processed_image)<\/p>\n

self.result_textinput.text = extracted_answers<\/p>\n

except Exception as e:<\/p>\n

error_popup = Popup(title=”\u9519\u8bef”, content=Label(text=str(e)), size_hint=(0.5, 0.5))<\/p>\n

\n

<\/figure>\n<\/p>\n

error_popup.open()<\/p>\n

“`<\/p>\n

\u73b0\u5728\uff0c\u6211\u4eec\u7684Python\u5b89\u5353\u5e94\u7528\u53ef\u4ee5\u8bc6\u522b\u7b54\u9898\u5361\u4e0a\u7684\u7b54\u6848\u4e86\u3002\u53ea\u9700\u8fd0\u884c`answer_recognizer.py`\uff0c\u7136\u540e\u5728Kivy\u5e94\u7528\u4e2d\u9009\u62e9\u5305\u542b\u7b54\u9898\u5361\u7684\u56fe\u7247\u5373\u53ef\u3002\u6210\u529f\u540e\uff0c\u5e94\u7528\u4f1a\u8bc6\u522b\u5e76\u5728\u6587\u672c\u6846\u4e2d\u663e\u793a\u51fa\u7b54\u6848\u3002<\/p>\n

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u91cc\u7684\u7b54\u9898\u5361\u8bc6\u522b\u4e3e\u4f8b\u7b80\u5316\u4e86\u7b97\u6cd5\uff0c\u5b9e\u9645\u5f00\u53d1\u65f6\u53ef\u80fd\u9700\u8981\u5bf9\u8bc6\u522b\u7b97\u6cd5\u505a\u66f4\u591a\u4f18\u5316\u548c\u8c03\u6574\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u6700\u540e\u4f7f\u7528Kivy\u4e3a\u5b89\u5353\u7f16\u8bd1\u6253\u5305\u7684\u8fc7\u7a0b\u5e76\u672a\u4ecb\u7ecd\uff0c\u5b9e\u9645\u5e94\u7528\u8bf7\u53c2\u8003Kivy\u5b98\u65b9\u6587\u6863\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728\u672c\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u5982\u4f55\u4f7f\u7528Python\u5f00\u53d1\u4e00\u4e2a\u5b89\u5353\u5e94\u7528\uff0c\u8be5\u5e94\u7528\u53ef\u4ee5\u8bc6\u522b\u7b54\u9898\u5361\u4e0a\u7684\u7b54\u6848\u3002\u6211\u4eec\u5c06\u91c7\u7528\u4e00\u4e2a\u540d\u4e3aKivy\u7684Python\u6a21\u5757\u6765\u5b9e\u73b0\u5b89\u5353App\u7684\u5f00\u53d1\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Kivy\u548c\u5176\u4ed6\u5fc5\u8981\u7684<\/p>\n","protected":false},"author":15,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[23858,23859,22123,23856,23857],"class_list":["post-17798","post","type-post","status-publish","format-standard","hentry","category-zuoapp","tag-mac-app","tag-23859","tag-app","tag-23856","tag-23857"],"_links":{"self":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/17798","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/comments?post=17798"}],"version-history":[{"count":1,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/17798\/revisions"}],"predecessor-version":[{"id":17834,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/17798\/revisions\/17834"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/media?parent=17798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/categories?post=17798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/tags?post=17798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}