微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 tkinter 中使用另一个 pyton 项目启动项目?

如何解决如何在 tkinter 中使用另一个 pyton 项目启动项目?

我是 tkinter 的新手。我有一个实时情绪检测项目。我从另一个 .py 文件调用,它们都在同一个文件中。视频捕获部分在 test.py 中定义了 cap,我调用了 cap 方法添加按钮。但是当我首先运行项目视频捕获工作时,当我关闭视频捕获修补程序窗口时,当我单击按钮时,它不起作用。我的代码错在哪里?我希望第一个 tkinter 窗口出现,当我点击按钮时,它开始视频捕获。我该怎么办?

from tkinter import *
from test import cap
root = Tk()
root.title('Emotion Detection')
root.iconbitmap(r'C:\Users\Doğukan\OneDrive\Masaüstü\ED\icon.ico')
root.geometry("500x300")
def run():
    return cap
myButton = Button(root,text="calistir",command=run(),padx=50)
myButton.pack(pady=20)
root.mainloop()

test.py

from keras.models import load_model
from time import sleep
from keras.preprocessing.image import img_to_array
from keras.preprocessing import image
import cv2
import numpy as np

face_classifier = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')
classifier =load_model('./Emotion_Detection.h5')

class_labels = ['Sinirli','Mutlu','Dogal','Uzgun','Saskin','Korkmus']

cap = cv2.VideoCapture(0)

while True:
    ret,frame = cap.read()
    labels = []
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    faces = face_classifier.detectMultiScale(gray,1.3,5)

    for (x,y,w,h) in faces:
        cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
        roi_gray = gray[y:y+h,x:x+w]
        roi_gray = cv2.resize(roi_gray,(48,48),interpolation=cv2.INTER_AREA)

        if np.sum([roi_gray])!=0:
            roi = roi_gray.astype('float')/255.0
            roi = img_to_array(roi)
            roi = np.expand_dims(roi,axis=0)

            preds = classifier.predict(roi)[0]
            print("\nprediction = ",preds)
            label=class_labels[preds.argmax()]
            print("\nprediction max = ",preds.argmax())
            print("\nlabel = ",label)
            label_position = (x,y)
            cv2.putText(frame,label,label_position,cv2.FONT_HERShey_SIMPLEX,2,(255,255),3)
        else:
            cv2.putText(frame,'Yuz Bulunamadi',(20,60),3)
        print("\n\n")
    frame= cv2.resize(frame,(860,490))    
    cv2.imshow('Emotion Detector',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'): # çıkma tuşu
        break

cap.release()
cv2.destroyAllWindows()

解决方法

将您的主脚本更改为:

from tkinter import *
from test import start_capturing
root = Tk()
root.title('Emotion Detection')
root.iconbitmap(r'C:\Users\Doğukan\OneDrive\Masaüstü\ED\icon.ico')
root.geometry("500x300")
def run():
    root.destroy() # Destroy the root so it doesn't hang
    start_capturing() # Start the function that is inside `test.py`
myButton = Button(root,text="calistir",command=run,padx=50)
myButton.pack(pady=20)
root.mainloop()

和您的 test.py 脚本:

from keras.models import load_model
from time import sleep
from keras.preprocessing.image import img_to_array
from keras.preprocessing import image
import cv2
import numpy as np

def start_capturing():
    face_classifier = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')
    classifier =load_model('./Emotion_Detection.h5')

    class_labels = ['Sinirli','Mutlu','Dogal','Uzgun','Saskin','Korkmus']

    cap = cv2.VideoCapture(0)

    while True:
        ret,frame = cap.read()
        labels = []
        gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
        faces = face_classifier.detectMultiScale(gray,1.3,5)

        for (x,y,w,h) in faces:
            cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
            roi_gray = gray[y:y+h,x:x+w]
            roi_gray = cv2.resize(roi_gray,(48,48),interpolation=cv2.INTER_AREA)

            if np.sum([roi_gray]) != 0:
                roi = roi_gray.astype("float") / 255.0
                roi = img_to_array(roi)
                roi = np.expand_dims(roi,axis=0)

                preds = classifier.predict(roi)[0]
                print("\nprediction = ",preds)
                label = class_labels[preds.argmax()]
                print("\nprediction max = ",preds.argmax())
                print("\nlabel = ",label)
                label_position = (x,y)
                cv2.putText(frame,label,label_position,cv2.FONT_HERSHEY_SIMPLEX,2,(255,255),3)
            else:
                cv2.putText(frame,'Yuz Bulunamadi',(20,60),3)
            print("\n\n")
        frame= cv2.resize(frame,(860,490))    
        cv2.imshow("Emotion Detector",frame)
        if cv2.waitKey(1) and (0xFF == ord("q")): # çıkma tuşu
            break

    cap.release()
    cv2.destroyAllWindows()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。