-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecognition.py
86 lines (66 loc) · 2.54 KB
/
recognition.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
import argparse
import sqlite3
import sys
import cv2
from fruit_detector.core import find_fruit_on_image
from fruit_detector.features import FeatureDetector
from fruit_detector.color_ranges import get_fruit_ranges
from fruit_detector.repositories import FeatureRepository
from fruit_detector.classifiers import Classifier
from fruit_detector.separators import ColorBasedImageSeparator
from fruit_detector.utils import get_jpg_from_directory, print_name_in_center
separator = ColorBasedImageSeparator()
detector = FeatureDetector()
classifier = Classifier()
def main():
args = get_args()
directory_path = get_directory_path(args)
db_path = get_db_path(args)
images_paths = get_images_paths(args, directory_path)
connection = sqlite3.connect(db_path)
feature_repository = FeatureRepository(connection)
fruit_color_ranges = get_fruit_ranges(connection)
for image_path in images_paths:
image = cv2.imread(image_path)
if image is None:
print('Unable to open image.')
sys.exit()
detected_fruits = find_fruit_on_image(image, fruit_color_ranges, feature_repository, separator, classifier,
detector)
for fruit_name, contour in detected_fruits:
print_name_in_center(contour, fruit_name, image)
cv2.imshow('Result', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
connection.close()
def get_args():
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", help="path to the image")
ap.add_argument("-d", "--directory", help="path to the directory with images of fruits")
ap.add_argument("-db", "--data_base", help="path to sqlite database file")
return vars(ap.parse_args())
def get_directory_path(args):
directoryPath = args.get("directory", False)
if not directoryPath:
return None
return directoryPath
def get_db_path(args):
db_path = args.get("data_base", False)
if not db_path:
return 'fruits.sqlite'
return db_path
def get_images_paths(args, directory_path):
if directory_path is None:
image_path = get_image_path(args)
image_paths = [image_path]
else:
image_paths = [directory_path + file_name for file_name in get_jpg_from_directory(directory_path)]
return image_paths
def get_image_path(args):
directoryPath = args.get("image", False)
if not directoryPath:
print('You must specify image using --image option.')
sys.exit()
return directoryPath
if __name__ == '__main__':
main()