This repository has been archived by the owner on Aug 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TA2020.py
172 lines (130 loc) · 5.23 KB
/
TA2020.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import layout
import tkinter as tk
import cv2 as cv
import copy
from brain import obj_detector as brain_obj_detector
from brainTracker import TrackerSystem
from tkinter import filedialog
from PIL import ImageTk, Image
class ObjectDetectorApp:
PHOTO = "Photo"
VIDEO = "Video"
DELAYWINDOW = 100
count_frame = 0
selectedFile = {
'path': None,
'name': None,
'format': None,
'type': None
}
def __init__(self, weight, cfg, names):
self.brain = brain_obj_detector(weight, cfg, names)
self.brain_tracker = TrackerSystem(weight, cfg, names)
def findFile(self):
FilePath = filedialog.askopenfilename()
# When user close filedialog
if len(FilePath) == 0:
return
photoFormats = ['png', 'jpg', 'jpeg', 'gif']
videoFormats = ['mp4', 'mkv', '3gp']
splitName = FilePath.split("/")
fileName = splitName[len(splitName)-1]
splitFormat = fileName.split(".")
# last index is format file
fileFormat = splitFormat[len(splitFormat)-1]
self.selectedFile['path'] = FilePath
self.selectedFile['name'] = fileName
self.selectedFile['format'] = fileFormat
print(FilePath)
if fileFormat in photoFormats:
self.selectedFile['type'] = self.PHOTO
elif fileFormat in videoFormats:
self.selectedFile['type'] = self.VIDEO
else:
self.selectedFile['type'] = None
self.showToPanel()
layout.setInfo(None)
def showToPanel(self):
layout.txtNameFile.set(self.selectedFile['name'])
if self.selectedFile['type'] == self.VIDEO:
self.showVideo2Panel()
return
self.showImage2Panel(self.selectedFile['path'])
def showImage2Panel(self, path=None, frame=None):
if path is not None:
loadImg = Image.open(path)
elif frame is not None:
loadImg = frame
wImage, hImage = loadImg.size
maxHeight = 700
adaptWidth = wImage * (maxHeight/hImage)
adaptHeight = maxHeight
loadImg = loadImg.resize((int(adaptWidth), int(adaptHeight)))
img = ImageTk.PhotoImage(loadImg)
layout.showImage.config(image=img)
layout.showImage.image = img
def showVideo2Panel(self):
cap = self.brain_tracker.capture_video(self.selectedFile['path'])
# cap = cv.VideoCapture(self.selectedFile['path'])
if cap == False:
layout.rootWindow.showerror("error", "file video tidak ditemukan")
return
frstFrame = self.brain_tracker.read_frame()
self.thumpnail = copy.deepcopy(frstFrame)
frstFrame = self.brain_tracker.cvrt_img(frstFrame)
frstFrame = Image.fromarray(frstFrame)
self.showImage2Panel(frame=frstFrame)
def runDetector(self):
if self.selectedFile['type'] == self.PHOTO:
self.detectorImage()
elif self.selectedFile['type'] == self.VIDEO:
self.detectorVideo()
else:
layout.rootWindow.showerror("error", "file video tidak ditemukan")
return False
def detectorImage(self):
layout.setProgressBar(10)
self.brain.set_photo(self.selectedFile['path'])
self.brain.label_obj() # process
result_img = self.brain.get_image()
# time process
time_consumn = self.brain.time_process
# generate from array to image
result_img = Image.fromarray(result_img)
self.showImage2Panel(frame=result_img)
layout.setProgressBar(100)
layout.setTxtLastSpd(time_consumn)
detectedObj = self.brain.listObj
layout.setInfo(detectedObj)
def detectorVideo(self):
self.updateDetectorVideo()
def updateDetectorVideo(self):
self.count_frame += 1
frame = self.brain_tracker.read_frame()
result = self.brain_tracker.track_object_inframe(
frame, self.count_frame)
# time process
time_consumn = self.brain_tracker.time_process
# generate from array to image
result_img = Image.fromarray(result)
self.showImage2Panel(frame=result_img)
layout.setTxtLastSpd(time_consumn)
detectedObj = self.brain_tracker.listObj
countedObj = self.brain_tracker.objects_counted
layout.setInfo(detectedObj, countedObj)
layout.rootWindow.after(self.DELAYWINDOW, self.updateDetectorVideo)
def selectROI(self):
layout.set_messagebox_info(
"Setting ROI", "Select area ROI and press ENTER to save it or ESC to cancel")
new_thumpnail = self.brain_tracker.set_ROI(self.thumpnail)
new_thumpnail = self.brain_tracker.cvrt_img(new_thumpnail)
new_thumpnail = Image.fromarray(new_thumpnail)
self.showImage2Panel(frame=new_thumpnail)
if __name__ == "__main__":
objDetector = ObjectDetectorApp('yolov3/yolov3.weights',
'yolov3/yolov3.cfg',
'yolov3/coco.names')
layout.runButton.config(command=objDetector.runDetector)
layout.chooseFile.config(command=objDetector.findFile)
layout.setROIbtn.config(command=objDetector.selectROI)
layout.rootWindow.mainloop() # put this at end file