【发布时间】:2022-01-12 17:34:11
【问题描述】:
我希望在 GUI 上按下按钮后打开相机,但不幸的是,当我运行代码时它会打开。我不知道问题出在哪里。
我是否可以将按钮放在凸轮画布旁边,以便在我实际按下按钮而不是“c”键时打印识别的文本。
import cv2
import pytesseract
import matplotlib.pyplot as plt
import numpy as np
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
import tkinter as tk
import cv2
from PIL import Image, ImageTk
class CamView():
def __init__(self, parent):
self.parent = parent
self.window = tk.Toplevel(parent)
self.lmain2 = tk.Label(self.window)
self.lmain2.pack()
self.window.protocol("WM_DELETE_WINDOW", self.close)
self.show_frame()
def show_frame(self):
imgtk = ImageTk.PhotoImage(image=self.parent.img)
self.lmain2.imgtk = imgtk
self.lmain2.configure(image=imgtk)
def close(self):
self.parent.test_frame = None
self.window.destroy()
root = tk.Tk()
root.bind('<Escape>', lambda e: root.quit())
class Main(tk.Frame):
def __init__(self, parent):
self.lmain = tk.Label(parent)
self.lmain.pack()
self.test_frame = None
frame = tk.Frame.__init__(self,parent)
a = tk.Label(text='hello!').pack()
b = tk.Button(frame, text='open', command=self.load_window)
b.pack()
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
font_scale = 2.5
font = cv2.FONT_HERSHEY_PLAIN
self.cap = cv2.VideoCapture(0)
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 426)
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 240)
if not self.cap.isOpened():
self.cap = cv2.VideoCapture(1)
if not self.cap.isOpened():
raise IOError("Cannot Open The Video Camera")
cntr = 0
while True:
ret, frame = self.cap.read()
cntr = cntr + 1
if ((cntr % 20) == 0):
imgH, imgW, _ = frame.shape
x1, y1, w1, h1 = 0, 0, imgH, imgW
imgchar = pytesseract.image_to_string(frame)
imgboxes = pytesseract.image_to_boxes(frame)
for boxes in imgboxes.splitlines():
boxes = boxes.split(' ')
x, y, w, h = int(boxes[1]), int(boxes[2]), int(boxes[3]), int(boxes[4])
cv2.rectangle(frame, (x, imgH - y), (w, imgH - h), (0, 0, 255), 3)
cv2.putText(frame, imgchar, (x1 + int(w1 / 50), y1 + int(h1 / 50)), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
(255, 0, 0), 2)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.imshow('Text detection', frame)
if cv2.waitKey(1) & 0xFF == ord('c'):
print(imgchar)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
self.do_stuff()
def do_stuff(self):
_, frame = self.cap.read()
frame = cv2.flip(frame, 1)
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
self.img = Image.fromarray(cv2image)
if self.test_frame != None:
self.test_frame.show_frame()
self.lmain.after(10, self.do_stuff)
def load_window(self):
if self.test_frame == None:
self.test_frame = CamView(self)
control = Main(root)
root.mainloop()
【问题讨论】:
-
“unf it open”是什么意思?我想你这里有一个错字。