1

I am new at tkinter.I have a real time emotion detection project.i called from another .py file, all of them is inside same file.Video capture part is defined cap in test.py, i call cap method and add the button.But when i run the project video capture works first and when i close the video capture tinker windows appear and when i clik the button, it doesn't work. Where is my fault in code? I want the first tkinter windows appear and when i clik the button its start the video capture. What should i do ?

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()
Doğukan CEBECİ
  • 300
  • 1
  • 3
  • 13

1 Answers1

1

Change your main script to:

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()

and your test.py script to:

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()
TheLizzard
  • 7,248
  • 2
  • 11
  • 31