如何在 tkinter 中使用另一个 python 项目启动项目?
How can start project with another pyton project in tkinter?
我是 tkinter.I 的新手,有一个从另一个 .py 文件调用的实时情绪检测 project.i,它们都在同一个 file.Video 中,捕获部分在 test.py,我调用 cap 方法并添加 button.But 当我 运行 项目视频捕获首先工作并且当我关闭视频捕获修补程序时 windows 出现并且当我点击按钮时, 它不起作用。我的代码错误在哪里?我想要第一个 tkinter windows 出现,当我点击按钮时它开始视频捕获。我该怎么办?
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,255),3)
else:
cv2.putText(frame,'Yuz Bulunamadi',(20,60),cv2.FONT_HERSHEY_SIMPLEX,2,(0,255,0),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, 255), 3)
else:
cv2.putText(frame,'Yuz Bulunamadi', (20, 60), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 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()
我是 tkinter.I 的新手,有一个从另一个 .py 文件调用的实时情绪检测 project.i,它们都在同一个 file.Video 中,捕获部分在 test.py,我调用 cap 方法并添加 button.But 当我 运行 项目视频捕获首先工作并且当我关闭视频捕获修补程序时 windows 出现并且当我点击按钮时, 它不起作用。我的代码错误在哪里?我想要第一个 tkinter windows 出现,当我点击按钮时它开始视频捕获。我该怎么办?
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,255),3)
else:
cv2.putText(frame,'Yuz Bulunamadi',(20,60),cv2.FONT_HERSHEY_SIMPLEX,2,(0,255,0),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, 255), 3)
else:
cv2.putText(frame,'Yuz Bulunamadi', (20, 60), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 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()