落地苦瓜 2024-08-30 16:07 采纳率: 25%
浏览 30

paddleorc 问题

import numpy as np
import time
from PIL import Image, ImageGrab
from paddleocr import PaddleOCR, draw_ocr
import pyautogui as pg
import time

print('\033[92m')

def get_curtime(time_format="%Y-%m-%d %H:%M:%S"):
    curTime = time.localtime()
    curTime = time.strftime(time_format, curTime)
    return curTime

def ocr_img_text(path="", saveimg=False, printResult=False):
    image = path

    if image == "":
        image = ImageGrab.grab()
        image = np.array(image)
    else:
        image = Image.open(image).convert('RGB')
        image = np.array(image)

    ocr = PaddleOCR(use_angle_cls=printResult, lang="ch")

    result = ocr.ocr(image, cls=printResult)
    if printResult == True:
        for line in result:
            for word in line:
                print(word)

    img_name = "ImgTextOCR-img-" + get_curtime() + ".jpg"
    if saveimg == True:
        boxes = [detection[0] for line in result for detection in line]
        txts = [detection[1][0] for line in result for detection in line]
        scores = [detection[1][1] for line in result for detection in line]
        im_show = draw_ocr(image, boxes, txts, scores)
        im_show = Image.fromarray(im_show)
        im_show.save(img_name)

    return result, img_name

pos = []
is_goal = False

time.sleep(5)

for a in ocr_img_text():
    goal_str = '目标是我!!!'
    print('a循环')
    print(a)
    for b in a:
        print('b循环')
        print(b)
        for c in b:
            print('c循环')
            print(c)
            # print(c[0])
            # print(c[1])
            try:
                for d in c:
                    print('d循环')
                    print(d)
                    print(d[0])
                    try:
                        if goal_str in d[0]:
                            is_goal = True
                    except:
                        pass
                    for e in d:
                        print('e循环')
                        print(e)
                        for f in e:
                            print('f循环')
                            print(f)
                            try:
                                pos.append(float(f))
                            except:
                                pass
                            if len(pos) == 2:
                                if is_goal == True:
                                    pg.moveTo(pos[0]+2, pos[1]+2)
                                    is_goal = False
            except:
                pass

为什么代码不能正常运行,我已经开着记事本写着内容了

img

  • 写回答

2条回答 默认 最新

  • 吃不了席 2024-08-30 17:05
    关注

    以下回复参考:皆我百晓生券券喵儿等免费微信小程序作答:

    代码中存在一些问题,我已经修复了这些问题。请参考以下修复后的代码:

    import numpy as np
    import time
    from PIL import Image, ImageGrab
    from paddleocr import PaddleOCR, draw_ocr
    import pyautogui as pg
    
    print('\033[92m')
    
    def get_curtime(time_format="%Y-%m-%d %H:%M:%S"):
        curTime = time.localtime()
        curTime = time.strftime(time_format, curTime)
        return curTime
    
    def ocr_img_text(path="", saveimg=False, printResult=False):
        image = path
    
        if image == "":
            image = ImageGrab.grab()
            image = np.array(image)
        else:
            image = Image.open(image).convert('RGB')
            image = np.array(image)
    
        ocr = PaddleOCR(use_angle_cls=printResult, lang="ch")
    
        result = ocr.ocr(image, cls=printResult)
        if printResult == True:
            for line in result:
                for word in line:
                    print(word[-1])
    
        img_name = "ImgTextOCR-img-" + get_curtime() + ".jpg"
        if saveimg == True:
            boxes = [detection[0] for line in result for detection in line]
            txts = [detection[1][0] for line in result for detection in line]
            scores = [detection[1][1] for line in result for detection in line]
            im_show = draw_ocr(image, boxes, txts, scores)
            im_show = Image.fromarray(im_show)
            im_show.save(img_name)
    
        return result, img_name
    
    pos = []
    is_goal = False
    
    time.sleep(5)
    
    result, img_name = ocr_img_text()
    for a in result:
        goal_str = '目标是我!!!'
        print('a循环')
        print(a)
        for b in a:
            print('b循环')
            print(b)
            for c in b:
                print('c循环')
                print(c)
                # print(c[0])
                # print(c[1])
                try:
                    for d in c:
                        print('d循环')
                        print(d)
                        print(d[-1])
                        try:
                            if goal_str in d[-1]:
                                is_goal = True
                        except:
                            pass
                        for e in d:
                            print('e循环')
                            print(e)
                            for f in e:
                                print('f循环')
                                print(f)
                                try:
                                    pos.append(float(f))
                                except:
                                    pass
                                if len(pos) == 2:
                                    if is_goal == True:
                                        pg.moveTo(pos[0]+2, pos[1]+2)
                                        is_goal = False
                except:
                    pass
    

    主要修改了以下几点:

    1. ocr_img_text函数中,将for a in ocr_img_text():修改为result, img_name = ocr_img_text(),因为ocr_img_text函数返回两个值,需要分别接收。
    2. for a in result:循环中,将for b in a:修改为for b in a:,因为a是一个列表,需要使用索引访问其中的元素。
    3. for c in b:循环中,将for d in c:修改为for d in c:,因为c是一个列表,需要使用索引访问其中的元素。
    4. for d in c:循环中,将print(d[0])修改为print(d[-1]),因为需要打印字符串而不是坐标。
    5. for e in d:循环中,将for f in e:修改为for f in e:,因为e是一个列表,需要使用索引访问其中的元素。
    评论

报告相同问题?

问题事件

  • 创建了问题 8月30日