-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.txt
More file actions
171 lines (132 loc) · 6.65 KB
/
backup.txt
File metadata and controls
171 lines (132 loc) · 6.65 KB
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
from easygui import *
import os
import tensorflow as tf
import cv2
import numpy as np
from object_detection.utils import config_util
from object_detection.protos import pipeline_pb2
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as viz_utils
from object_detection.builders import model_builder
from object_detection.utils import config_util
from google.protobuf import text_format
from matplotlib import pyplot as plt
# my_ssd_mobnet_tuned
# my_lite_640_ssd
CUSTOM_MODEL_NAME = 'my_ssd_mobnet_tuned'
TF_RECORD_SCRIPT_NAME = 'generate_tfrecord.py'
LABEL_MAP_NAME = 'label_map.pbtxt'
check_pt = 'ckpt-21'
# https://www.geeksforgeeks.org/python-easygui-enter-box/
#
paths = {
'SCRIPTS_PATH': os.path.join('Tensorflow','scripts'),
'ANNOTATION_PATH': os.path.join('Tensorflow', 'workspace','annotations'),
'MODEL_PATH': os.path.join('Tensorflow', 'workspace','models'),
'CHECKPOINT_PATH': os.path.join('Tensorflow', 'workspace','models',CUSTOM_MODEL_NAME, check_pt),
}
files = {
'PIPELINE_CONFIG':os.path.join('Tensorflow', 'workspace','models', CUSTOM_MODEL_NAME, 'pipeline.config'),
'TF_RECORD_SCRIPT': os.path.join(paths['SCRIPTS_PATH'], TF_RECORD_SCRIPT_NAME),
'LABELMAP': os.path.join(paths['ANNOTATION_PATH'], LABEL_MAP_NAME)
}
# /-----------------UI---------------/
# camera_cap = input("Please type the camera number you wish to use, 0 is first cam: \n")
# print(f"Here are the file paths that are being used: \nFor model path: {files['PIPELINE_CONFIG']}\nFor check point path: {paths['CHECKPOINT_PATH']}")
# change_path = input("would you like to change any of the paths above? yb for both, yc for check point yp for model, n for no \n")
response = ["ya","yl","yc","yp","n"]
# while (change_path not in response):
# change_path = input("would you like to change any of the paths above? ya for all, yl for label map, yc for check point yp for model, n for no \n")
# message to be displayed
text = "Please type the camera number you wish to use, 0 is first cam"
title = "PC Interactor"
# default text
d_text = ""
# creating a enter box
camera_cap = enterbox(text, title, d_text)
# title for the message box
title = "Message Box"
# creating a message
message = f"you chose : {camera_cap} for cam. Here are the file paths that are being used: \nFor model path: {files['PIPELINE_CONFIG']}\nFor check point path: {paths['CHECKPOINT_PATH']} \nfor label map : {files['LABELMAP']}"
msg = msgbox(message, title)
# change_path = enterbox(message, title, d_text)
change_message = "would you like to change any of the paths above? ya for all, yl for label map, yc for check point yp for model, n for no"
change_path = enterbox(change_message, title, d_text)
while (change_path not in response):
change_path = enterbox(change_message, title, d_text)
# change_path = input("would you like to change any of the paths above? ya for all, yl for label map, yc for check point yp for model, n for no \n")
# creating a message box
# msg = msgbox(message, title)
# response functions can be better with dict mapping to response [TODO if time allows]
if(change_path == "n"):
pass
elif(change_path == "ya"):
msgbox("pipline / model \n")
files['PIPELINE_CONFIG'] = fileopenbox()
msgbox("Labelmap: \n")
files['LABELMAP'] = fileopenbox()
msgbox("check point path Directory: \n")
paths['CHECKPOINT_PATH'] = diropenbox()
num = enterbox(f"checkpoint number? ","","")
paths['CHECKPOINT_PATH'] = os.path.join(paths['CHECKPOINT_PATH'], f'ckpt-{num}')
elif(change_path == "yc"):
msgbox("check point path Directory: \n")
paths['CHECKPOINT_PATH'] = diropenbox()
num = enterbox(f"checkpoint number? ","","")
paths['CHECKPOINT_PATH'] = os.path.join(paths['CHECKPOINT_PATH'], f'ckpt-{num}')
elif(change_path == "yp"):
msgbox("pipline / model \n")
files['PIPELINE_CONFIG'] = fileopenbox()
elif(change_path == "yl"):
msgbox("label map path: \n")
files['LABELMAP'] = fileopenbox()
program_settings = {'cam':camera_cap, 'label_map':files['LABELMAP'], 'model':files['PIPELINE_CONFIG'],"check point":paths['CHECKPOINT_PATH']}
msgbox(f"Here are the settings you chose: {camera_cap}, label map: {files['LABELMAP']}, model: {files['PIPELINE_CONFIG']}, check point {paths['CHECKPOINT_PATH']}")
# msgbox(f"Here are the settings you chose: {program_settings}")
def main():
category_index = label_map_util.create_category_index_from_labelmap(files['LABELMAP'])
# Load pipeline config and build a detection model
configs = config_util.get_configs_from_pipeline_file(files['PIPELINE_CONFIG'])
detection_model = model_builder.build(model_config=configs['model'], is_training=False)
# Restore checkpoint (latest model)
ckpt = tf.compat.v2.train.Checkpoint(model=detection_model)
ckpt.restore(paths['CHECKPOINT_PATH']).expect_partial()
# ckpt.restore(paths['CHECKPOINT_PATH'])
@tf.function
def detect_fn(image):
image, shapes = detection_model.preprocess(image)
prediction_dict = detection_model.predict(image, shapes)
detections = detection_model.postprocess(prediction_dict, shapes)
return detections
cap = cv2.VideoCapture(int(camera_cap))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
while cap.isOpened():
ret, frame = cap.read()
image_np = np.array(frame)
input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
detections = detect_fn(input_tensor)
num_detections = int(detections.pop('num_detections'))
detections = {key: value[0, :num_detections].numpy()
for key, value in detections.items()}
detections['num_detections'] = num_detections
# detection_classes should be ints.
detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
label_id_offset = 1
image_np_with_detections = image_np.copy()
viz_utils.visualize_boxes_and_labels_on_image_array(
image_np_with_detections,
detections['detection_boxes'],
detections['detection_classes']+label_id_offset,
detections['detection_scores'],
category_index,
use_normalized_coordinates=True,
max_boxes_to_draw=5,
min_score_thresh=.65 ,
agnostic_mode=False)
cv2.imshow('object detection', cv2.resize(image_np_with_detections, (800, 600)))
if cv2.waitKey(10) & 0xFF == ord('q'):
cap.release()
cv2.destroyAllWindows()
break
main()