Skip to content

Commit 86fc230

Browse files
Cleaning the code
1 parent fc60f9b commit 86fc230

File tree

6 files changed

+595
-401
lines changed

6 files changed

+595
-401
lines changed

capture.py

Lines changed: 79 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,122 @@
1-
"""Script to capture images from object detection"""
1+
"""Script to capture images from object detection."""
22

33
import argparse
4-
from utils import video
4+
from utils import video, terminal
55
from detection import deeplearning, traditional
66

77

88
def parse_argument():
9-
"""Parse arguments for command line"""
9+
"""Parse arguments for command line."""
1010
parser = argparse.ArgumentParser(
11-
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
11+
formatter_class=argparse.ArgumentDefaultsHelpFormatter
12+
)
1213
parser.add_argument(
13-
'--method',
14-
help='Two computer vision method: traditional or deeplearning',
14+
"--method",
15+
help="Two computer vision method: traditional or deeplearning",
1516
required=False,
1617
type=str,
17-
default='deeplearning')
18+
default="deeplearning",
19+
)
1820
parser.add_argument(
19-
'--detectionType',
20-
help='Type of detection for traditional method: color OR shape',
21+
"--detectionType",
22+
help="Type of detection for traditional method: color OR shape",
2123
required=False,
22-
default='color')
24+
default="color",
25+
)
2326
parser.add_argument(
24-
'--multipleObject',
25-
help='Whether to detect single or multiple types of objects.',
27+
"--multipleObject",
28+
help="Whether to detect single or multiple types of objects.",
2629
required=False,
27-
default=False)
30+
default=False,
31+
)
2832
parser.add_argument(
29-
'--trueClass',
30-
help='Name of the object class to be detected if it is a single type.',
33+
"--trueClass",
34+
help="Name of the object class to be detected if it is a single type.",
3135
required=False,
3236
type=str,
33-
default='yellow_duck')
37+
default="yellow_duck",
38+
)
3439
parser.add_argument(
35-
'--collectAll',
36-
help='Whether to collect all images or only the True Positive one.',
40+
"--collectAll",
41+
help="Whether to collect all images or only the True Positive one.",
3742
required=False,
38-
default=False)
43+
default=False,
44+
)
3945
parser.add_argument(
40-
'--model',
41-
help='Path of the object detection model.',
46+
"--model",
47+
help="Path of the object detection model.",
4248
required=False,
43-
default='./model/frogducky2.tflite')
49+
default="./model/frogducky2.tflite",
50+
)
4451
parser.add_argument(
45-
'--frameWidth',
46-
help='Width of frame to capture from camera.',
52+
"--frameWidth",
53+
help="Width of frame to capture from camera.",
4754
required=False,
4855
type=int,
49-
default=640)
56+
default=640,
57+
)
5058
parser.add_argument(
51-
'--frameHeight',
52-
help='Height of frame to capture from camera.',
59+
"--frameHeight",
60+
help="Height of frame to capture from camera.",
5361
required=False,
5462
type=int,
55-
default=480)
63+
default=480,
64+
)
5665
parser.add_argument(
57-
'--numThreads',
58-
help='Number of CPU threads to run the model.',
66+
"--numThreads",
67+
help="Number of CPU threads to run the model.",
5968
required=False,
6069
type=int,
61-
default=4)
70+
default=4,
71+
)
6272
parser.add_argument(
63-
'--enableEdgeTPU',
64-
help='Whether to run the model on EdgeTPU.',
65-
action='store_true',
73+
"--enableEdgeTPU",
74+
help="Whether to run the model on EdgeTPU.",
75+
action="store_true",
6676
required=False,
67-
default=False)
77+
default=False,
78+
)
6879
parser.add_argument(
69-
'--videoFilename',
70-
help='Name of the output video file with .mp4 extension',
80+
"--videoFilename",
81+
help="Name of the output video file with .mp4 extension",
7182
required=False,
72-
default='Deteksi_Objek.mp4')
83+
default="Deteksi_Objek.mp4",
84+
)
7385
return parser.parse_args()
7486

7587

7688
def main():
77-
"""Main function to run detection"""
89+
"""Run detection and capture the result."""
7890
args = parse_argument()
79-
80-
if args.method == 'deeplearning':
81-
detector = deeplearning.DeepDetector(bool(args.multipleObject), args.model,
82-
args.frameWidth, args.frameHeight,
83-
int(args.numThreads),bool(args.enableEdgeTPU))
84-
detector.capture(str(args.trueClass), bool(args.collectAll), str(args.videoFilename))
85-
86-
elif args.method == 'traditional':
87-
if str(args.detectionType) == 'category':
91+
92+
if args.method == "deeplearning":
93+
detector = deeplearning.DeepDetector(
94+
bool(args.multipleObject),
95+
args.model,
96+
args.frameWidth,
97+
args.frameHeight,
98+
int(args.numThreads),
99+
bool(args.enableEdgeTPU),
100+
)
101+
detector.capture(
102+
str(args.trueClass), bool(args.collectAll), str(args.videoFilename)
103+
)
104+
105+
elif args.method == "traditional":
106+
if str(args.detectionType) == "category":
88107
args.detectionType = terminal.prompt_type()
89-
detector = traditional.TraditionalDetector(bool(args.multipleObject), args.frameWidth,
90-
args.frameHeight)
91-
detector.capture(str(args.detectionType), str(args.trueClass),
92-
bool(args.collectAll), str(args.videoFilename))
93-
108+
detector = traditional.TraditionalDetector(
109+
bool(args.multipleObject), args.frameWidth, args.frameHeight
110+
)
111+
detector.capture(
112+
str(args.detectionType),
113+
str(args.trueClass),
114+
bool(args.collectAll),
115+
str(args.videoFilename),
116+
)
117+
94118
video.move_video(str(args.videoFilename))
95119

96120

97-
if __name__ == '__main__':
98-
main()
121+
if __name__ == "__main__":
122+
main()

collect.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import os
1+
"""Scripts to collect images without running a detection."""
2+
23
import sys
34

4-
import numpy as np
55
import cv2
66

77
IMG_ADDR = "/home/pi/Desktop/Object Detection/Raspberry Pi"
@@ -17,19 +17,20 @@
1717
success, image = cap.read()
1818
if not success:
1919
sys.exit(
20-
'ERROR: Unable to read from webcam. Please verify your webcam settings.'
20+
"""ERROR: Unable to read from webcam.
21+
Please verify your webcam settings."""
2122
)
22-
23+
2324
counter += 1
24-
25-
#image = cv2.flip(image, 1)
26-
# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
25+
26+
# image = cv2.flip(image, 1)
27+
# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
2728
cv2.imwrite(IMG_ADDR + "/image/Image" + str(counter) + ".png", image)
2829

2930
# Stop the program if the ESC key is pressed.
3031
if cv2.waitKey(1) == 27:
3132
break
32-
33+
3334
cv2.imshow("Captured Image", image)
34-
35-
cv2.destroyWindow("Captured Image")
35+
36+
cv2.destroyWindow("Captured Image")

0 commit comments

Comments
 (0)