我有一个关于使用tensorflow的面罩检测的最后一年项目。我在互联网上找到了这个源代码,我想添加一些功能,如音频反馈,当有人不戴口罩,并采取他们的照片。如何自定义代码?
import cv2
import numpy as np
category_index = label_map_util.create_category_index_from_labelmap(ANNOTATION_PATH+'/label_map.pbtxt')
# Setup capture
cap = cv2.VideoCapture(0)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
while True:
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=.7,
agnostic_mode=False)
cv2.imshow('Face Mask Detection', cv2.resize(image_np_with_detections, (960, 720)))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
cam = cv2.VideoCapture(0)
img_counter = 0
而真实的:Ret, frame = cam.read()
if not ret:
print("gagal foto")
break
cv2.imshow("selfie", frame)
k = cv2.waitKey(1)
if k%256 == 27:
# ESC pressed
print("pencet esc")
break
elif k%256 == 32:
# SPACE pressed
img_name = "no{}.png".format(img_counter)
cv2.imwrite(img_name, frame)
print("{} written!".format(img_name))
img_counter += 1
cam.release ()
cv2.destroyAllWindows ()