-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
68 lines (62 loc) · 2.85 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import cv2
from PIL import Image, ImageTk
from tkinter import filedialog
import constants
def open_file_dialog(selection, app):
if selection == 'Image':
constants.IMG_PATH = filedialog.askopenfilename()
print_file_path(selection, app)
elif selection == 'Video':
constants.VIDEO_PATH = filedialog.askopenfilename()
print_file_path(selection, app)
def print_file_path(selection, app):
IMG_PATH = constants.IMG_PATH
VIDEO_PATH = constants.VIDEO_PATH
if selection == 'Image':
if IMG_PATH.endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff')):
app.image_path_header.configure(text="Currently selected image", text_color="green")
app.image_error_path_header.configure(text="")
app.image_path.configure(text=IMG_PATH)
app.image_detect_button.place(relx=0.5, rely=0.85, anchor="center")
elif IMG_PATH == "":
app.image_error_path_header.configure(text="No file selected", text_color="red")
app.image_path_header.configure(text="")
app.image_path.configure(text="")
app.image_detect_button.place_forget()
else:
app.image_error_path_header.configure(text="Invalid file format", text_color="red")
app.image_path_header.configure(text="")
app.image_path.configure(text="")
app.image_detect_button.place_forget()
elif selection == 'Video':
if VIDEO_PATH.endswith(('.mp4', '.avi', '.mov', '.flv', '.wmv', '.mkv')):
app.video_path_header.configure(text="Currently selected video", text_color="green")
app.video_error_path_header.configure(text="")
app.video_path.configure(text=VIDEO_PATH)
app.video_detect_button.place(relx=0.5, rely=0.85, anchor="center")
elif VIDEO_PATH == "":
app.video_error_path_header.configure(text="No file selected", text_color="red")
app.video_path_header.configure(text="")
app.video_path.configure(text="")
app.video_detect_button.place_forget()
else:
app.video_error_path_header.configure(text="Invalid file format", text_color="red")
app.video_path_header.configure(text="")
app.video_path.configure(text="")
app.video_detect_button.place_forget()
def cv2_to_imagetk(img_path):
image = cv2.imread(img_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
imagePIL = Image.fromarray(image)
imageTk = ImageTk.PhotoImage(image=imagePIL)
return imageTk
def back(event, window, type, cap, app):
if window.winfo_containing(event.x_root, event.y_root) == event.widget:
if type == "video":
cap.release()
app.deiconify()
return
if type == "webcam":
cap.release()
app.deiconify()
window.destroy()