简体   繁体   English

如何将 Opencv 集成到 Tkinter 窗口中

[英]How can I integrate Opencv into a Tkinter window

I'm trying to put face recogniton code that uses opencv to the left hand side of a tkinter window.我正在尝试将使用 opencv 的面部识别代码放在 tkinter 窗口的左侧。 By doing this I wish to leave the right hand side of the window free so I can output text.通过这样做,我希望让窗口的右侧空闲,以便我可以输出文本。 eg when a face is detected the program will display "Name: Present" I am new to both Tkinter and OpenCV andI can't seem to find a straight answer online.例如,当检测到人脸时,程序将显示“名称:存在”我是 Tkinter 和 OpenCV 的新手,我似乎无法在网上找到直接的答案。 Any Help is appreciated, thanks!任何帮助表示赞赏,谢谢!

Here is my code below:这是我的代码如下:

import face_recognition
import cv2
import numpy as np
import tkinter
from tkinter import *
import PySimpleGUI as sg
import xlsxwriter
import os
from PIL import ImageTk,Image
from datetime import datetime;
import datetime


#Defines time
now = datetime.datetime.now().time()

#Setup for period segment of spreadsheetname
if now.hour<9:
    name = "HomeRoom "
elif now.hour==9 and now.min<=50:
    name = "Period1 "
elif now.hour==10 and now.min<=40:
    name = "Period2 "
elif now.hour==11 and now.min<=50:
    name = "Period3 "
elif now.hour==12 and now.min<=40:
    name = "Period4 "
elif now.hour==14 and now.min<=10:
    name = "Period5 "
elif now.hour<=15:
    name = "Period6 "
else:
    name = "Testing "



# Webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)

# to break loop
Printed = False

#Defines todays date                               #day/month/year-HourAM/PM
todays_date = str(datetime.datetime.now().strftime("%d-%m-%Y %I%p"))


#Sets up spreadsheet
workbook = xlsxwriter.Workbook(name + todays_date +'.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'Name')
worksheet.write('B1', 'Attendance')
worksheet.write('A6', 'Jordan Terzian')
worksheet.write('B6', 'Absent')
worksheet.write('A5', 'Daniel Pearce')
worksheet.write('B5', 'Absent')
worksheet.write('A4', 'Ewan Krall')
worksheet.write('B4', 'Absent')
worksheet.write('A3', 'Norman Brosow')
worksheet.write('B3', 'Absent')
worksheet.write('A2', 'Mitchell Benson')
worksheet.write('B2', 'Absent')

# classmates

jordan_image = face_recognition.load_image_file("jordan.jpg")
jordan_face_encoding = face_recognition.face_encodings(jordan_image)[0]

daniel_image = face_recognition.load_image_file("daniel.jpg")
daniel_face_encoding = face_recognition.face_encodings(daniel_image)[0]

ewan_image = face_recognition.load_image_file("ewan.jpg")
ewan_face_encoding = face_recognition.face_encodings(ewan_image)[0]

norman_image = face_recognition.load_image_file("norman.jpg")
norman_face_encoding = face_recognition.face_encodings(norman_image)[0]

mitch_image = face_recognition.load_image_file("mitch.jpg")
mitch_face_encoding = face_recognition.face_encodings(mitch_image)[0]



# Create arrays of known face encodings and their names
known_face_encodings = [
    jordan_face_encoding,
    daniel_face_encoding,
    ewan_face_encoding,
    norman_face_encoding,
    mitch_face_encoding,

]
known_face_names = [
    "Jordan Terzian",
    "Daniel Pearce",
    "Ewan Krall",
    "Norman Brosow",
    "Mitchell Benson",

]



# Initialize variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

while True:
    # Grab a single frame of video
    ret, frame = video_capture.read()

    # Resize frame of video to 1/4 size for faster face recognition processing
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

    # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
    rgb_small_frame = small_frame[:, :, ::-1]


    # Only process every other frame of video to save time
    if process_this_frame:
        # Find all the faces and face encodings in the current frame of video
        face_locations = face_recognition.face_locations(rgb_small_frame)
        face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

        face_names = []
        for face_encoding in face_encodings:
            # See if the face is a match for the known face(s)
            matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
            name = "Unknown"

            # use the known face with the smallest distance to the new face
            face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
            best_match_index = np.argmin(face_distances)
            if matches[best_match_index]:
                name = known_face_names[best_match_index]

            face_names.append(name)

    process_this_frame = not process_this_frame


    # Display the results
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        # Scale back up face locations since the frame we detected in was scaled to 1/4 size
        top *= 4
        right *= 4
        bottom *= 4
        left *= 4

        # Draw a box around the face
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

        # Draw a label with a name below the face,
        cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
        #Writes to spreadsheet and GUI
        if name == "Jordan Terzian" and not Printed:
            print("Jordan Terzian is Present")
            Printed = True
            worksheet.write('B6', 'Present')
        elif name == "Daniel Pearce" and not Printed:
            print("Daniel Pearce is Present")
            Printed = True
            worksheet.write('B5', 'Present')
        elif name == "Ewan Krall" and not Printed:
            print("Ewan Krall is Present")
            Printed = True
            worksheet.write('B4', 'Present')
        elif name == "Norman Brosow" and not Printed:
            print("Norman Brosow is Present")
            Printed = True
            worksheet.write('B3', 'Present')
        elif name == "Mitchell Benson" and not Printed:
            print("Michell Benson is Present")
            Printed = True
            worskheet.write('B2', 'Present')

    # Display the resulting image
    cv2.imshow('Video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release handle to the webcam, Closes webcam
video_capture.release()
cv2.destroyAllWindows()
workbook.close()

It is simple example which gets frame from cv2 and replaces it in PhotoImage which is displayed on Canvas .这是简单的例子,其获取framecv2和替换它PhotoImage ,其被显示在Canvas It uses after() to run function update_frame() periodically so it doesn't block root.mainloop() which has to run all time.它使用after()定期运行函数update_frame()因此它不会阻止root.mainloop()运行的root.mainloop()

You will have to run code from while True in function update_frame() without using while True您必须在函数update_frame()while True运行代码而不使用while True

import tkinter as tk
from PIL import Image, ImageTk
import cv2

# --- functions ---

def update_frame():

    ret, frame = cap.read()

    image = Image.fromarray(frame)
    photo.paste(image)

    #description['text'] = 'new text'

    root.after(10, update_frame) # update it again after 10ms

# --- main ---

cap = cv2.VideoCapture(0)

# get first frame
ret, frame = cap.read()

# - GUI -

root = tk.Tk()

image = Image.fromarray(frame)
photo = ImageTk.PhotoImage(image)  # it has to be after `tk.Tk()`

canvas = tk.Canvas(root, width=photo.width(), height=photo.height())
canvas.pack(side='left', fill='both', expand=True)

canvas.create_image((0,0), image=photo, anchor='nw')

description = tk.Label(root, text="Place for description")
description.pack(side='right')

# - start -

update_frame() # update it first time

root.mainloop() # start program - this loop runs all time

# - after close -

cap.release()

BTW: I have example with buttons Play , Stop , Save Image : python-examples/cv2/tkinter-CV顺便说一句:我有按钮PlayStopSave Image 的例子python-examples/cv2/tkinter-CV


EDIT: I can test it but it could be something like this.编辑:我可以测试它,但它可能是这样的。

import face_recognition
import cv2
import numpy as np
import tkinter
#from tkinter import * # PEP8: `import *` is not preferred
#import PySimpleGUI as sg #
import xlsxwriter
import os
from PIL import ImageTk, Image
#from datetime import datetime;
import datetime

# --- functions ---

def process_frame():
    global process_this_frame

    # Grab a single frame of video
    ret, frame = video_capture.read()

    # Resize frame of video to 1/4 size for faster face recognition processing
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

    # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
    rgb_small_frame = small_frame[:, :, ::-1]

    # Only process every other frame of video to save time
    if process_this_frame:
        # Find all the faces and face encodings in the current frame of video
        face_locations = face_recognition.face_locations(rgb_small_frame)
        face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

        face_names = []
        for face_encoding in face_encodings:
            # See if the face is a match for the known face(s)
            matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
            name = "Unknown"

            # use the known face with the smallest distance to the new face
            face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
            best_match_index = np.argmin(face_distances)
            if matches[best_match_index]:
                name = known_face_names[best_match_index]

            face_names.append(name)

    process_this_frame = not process_this_frame


    # Display the results
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        # Scale back up face locations since the frame we detected in was scaled to 1/4 size
        top *= 4
        right *= 4
        bottom *= 4
        left *= 4

        # Draw a box around the face
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

        # Draw a label with a name below the face,
        cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
        #Writes to spreadsheet and GUI
        if name == "Jordan Terzian" and not Printed:
            print("Jordan Terzian is Present")
            Printed = True
            worksheet.write('B6', 'Present')
        elif name == "Daniel Pearce" and not Printed:
            print("Daniel Pearce is Present")
            Printed = True
            worksheet.write('B5', 'Present')
        elif name == "Ewan Krall" and not Printed:
            print("Ewan Krall is Present")
            Printed = True
            worksheet.write('B4', 'Present')
        elif name == "Norman Brosow" and not Printed:
            print("Norman Brosow is Present")
            Printed = True
            worksheet.write('B3', 'Present')
        elif name == "Mitchell Benson" and not Printed:
            print("Michell Benson is Present")
            Printed = True
            worskheet.write('B2', 'Present')

        description['text'] = name

    image = Image.fromarray(frame)
    photo.paste(image)

    root.after(40, process_frame) # update it again after 40ms - it gives 25 FPS (1000ms/40ms=25)

# --- init ---

#Defines time
now = datetime.datetime.now().time()

#Setup for period segment of spreadsheetname
if now.hour<9:
    name = "HomeRoom "
elif now.hour==9 and now.min<=50:
    name = "Period1 "
elif now.hour==10 and now.min<=40:
    name = "Period2 "
elif now.hour==11 and now.min<=50:
    name = "Period3 "
elif now.hour==12 and now.min<=40:
    name = "Period4 "
elif now.hour==14 and now.min<=10:
    name = "Period5 "
elif now.hour<=15:
    name = "Period6 "
else:
    name = "Testing "

# to break loop
Printed = False

#Defines todays date                               #day/month/year-HourAM/PM
todays_date = str(datetime.datetime.now().strftime("%d-%m-%Y %I%p"))


#Sets up spreadsheet
workbook = xlsxwriter.Workbook(name + todays_date +'.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'Name')
worksheet.write('B1', 'Attendance')
worksheet.write('A6', 'Jordan Terzian')
worksheet.write('B6', 'Absent')
worksheet.write('A5', 'Daniel Pearce')
worksheet.write('B5', 'Absent')
worksheet.write('A4', 'Ewan Krall')
worksheet.write('B4', 'Absent')
worksheet.write('A3', 'Norman Brosow')
worksheet.write('B3', 'Absent')
worksheet.write('A2', 'Mitchell Benson')
worksheet.write('B2', 'Absent')

# classmates

#jordan_image = face_recognition.load_image_file("jordan.jpg")
#jordan_face_encoding = face_recognition.face_encodings(jordan_image)[0]
#
#daniel_image = face_recognition.load_image_file("daniel.jpg")
#daniel_face_encoding = face_recognition.face_encodings(daniel_image)[0]
#
#ewan_image = face_recognition.load_image_file("ewan.jpg")
#ewan_face_encoding = face_recognition.face_encodings(ewan_image)[0]
#
#norman_image = face_recognition.load_image_file("norman.jpg")
#norman_face_encoding = face_recognition.face_encodings(norman_image)[0]
#
#mitch_image = face_recognition.load_image_file("mitch.jpg")
#mitch_face_encoding = face_recognition.face_encodings(mitch_image)[0]

# Create arrays of known face encodings and their names
#known_face_encodings = [
#    jordan_face_encoding,
#    daniel_face_encoding,
#    ewan_face_encoding,
#    norman_face_encoding,
#    mitch_face_encoding,
#]

filenames = [
    "jordan.jpg",
    "daniel.jpg",
    "ewan.jpg",
    "norman.jpg",
    "mitch.jpg"
]

known_face_encodings = []

for name in filenames:
    image = face_recognition.load_image_file(name)
    face_encoding = face_recognition.face_encodings(image)[0]
    known_face_encodings.append(face_encoding)

known_face_names = [
    "Jordan Terzian",
    "Daniel Pearce",
    "Ewan Krall",
    "Norman Brosow",
    "Mitchell Benson",
]

# Initialize variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

# --- main ---

# Webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)

# get first frame to get size
ret, frame = cap.read()

# - GUI -

root = tk.Tk()

image = Image.fromarray(frame)
photo = ImageTk.PhotoImage(image)  # it has to be after `tk.Tk()`

canvas = tk.Canvas(root, width=photo.width(), height=photo.height())
canvas.pack(side='left', fill='both', expand=True)

canvas.create_image((0,0), image=photo, anchor='nw')

description = tk.Label(root, text="Place for description")
description.pack(side='right')

# - start -

process_frame() # update it first time

root.mainloop() # start program - this loop runs all time

# --- end ---

# Release handle to the webcam, Closes webcam
video_capture.release()
#cv2.destroyAllWindows()
workbook.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM