opencv imshow 没有响应

发布于 2025-01-15 09:29:25 字数 1048 浏览 0 评论 0原文

代码的目标:

我使用 opencv 和 pytesseract 对某些图像进行 OCR。由于我无法控制图像质量和清晰度,我需要检测不完整的 OCR 并自己完成这项工作。因此,我尝试使用 cv2.imshow() 和 input() 查询来显示图像。

问题:

imshow 窗口打开但没有响应,仅显示灰色。如果我完成任何输入,代码就会恢复工作。

            cv2.imwrite("count.png", count_zone)
            zone = cv2.imread("count.png")

            # read numbers with pytesseract


            gray = cv2.cvtColor(zone, cv2.COLOR_BGR2GRAY)

            _, threshold = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

            ocr = pytesseract.image_to_string(Image.open(config.zone_root))
            value = ''.join(re.findall('[0-9]+', ocr))

            #todo: find a way to close the image once the query is entered

            if value == "":
                
                cv2.imshow("Value", zone)
                print("ENTRE THE VALUE:")
                value = input()

                cv2.destroyWindow("Value")

经过测试的解决方案:

  1. 如果我使用 Pillow,我可以读取图像,但关闭它是一个问题
  2. 我尝试将 imshow 放在第 3 行中,同样的问题
  3. 如果我将输入放在注释中,代码可以完美运行

Objective of the code:

I'm using opencv and pytesseract to OCR some image. Due to the fact I can't control image quality and definition, I need to detect incomplete OCR and do the job myself. So I try to show the image using cv2.imshow() followed by an input() query.

Problem:

The imshow windows is opening but not responding, showing only grey. Code get back to work if I complete any input.

            cv2.imwrite("count.png", count_zone)
            zone = cv2.imread("count.png")

            # read numbers with pytesseract


            gray = cv2.cvtColor(zone, cv2.COLOR_BGR2GRAY)

            _, threshold = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

            ocr = pytesseract.image_to_string(Image.open(config.zone_root))
            value = ''.join(re.findall('[0-9]+', ocr))

            #todo: find a way to close the image once the query is entered

            if value == "":
                
                cv2.imshow("Value", zone)
                print("ENTRE THE VALUE:")
                value = input()

                cv2.destroyWindow("Value")

Tested solution:

  1. If I use Pillow, I can read the image but closing it is a problem
  2. I tried to put imshow in line 3, same issue
  3. If I put input in comment, the code works perfectly

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挖鼻大婶 2025-01-22 09:29:25

如果使用Spyder

打开工具栏中的“工具”,然后打开“首选项”。

在打开的窗口中,单击左侧菜单中的“IPython 控制台”设置。

单击新打开的窗口中的“图形”选项卡。
将“后端”设置为“自动”,然后按“应用”然后是“好的”。

您可以通过在 Spyder 工具栏的“文件”菜单中执行“重新启动”来找到解决方案。

If using Spyder:

Open 'Tools' then 'Prefences' in the toolbar.

In the window that opens, click the 'IPython console' setting from the menu on the left.

Click the 'Graphics' tab in the newly opened window.
Set 'Backend' to 'Automatic' and press 'Apply' then 'Ok'.

You can find the solution by doing 'Restart' in the 'File' menu in the Spyder toolbar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文