|
import cv2 |
|
import numpy as np |
|
from tensorflow.lite.python.interpreter import Interpreter |
|
|
|
def tflite_detect_webcam(modelpath, lblpath, min_conf=0.5, txt_only=False, frame_skip=5): |
|
|
|
with open(lblpath, 'r') as f: |
|
labels = [line.strip() for line in f.readlines()] |
|
|
|
|
|
interpreter = Interpreter(model_path=modelpath) |
|
interpreter.allocate_tensors() |
|
|
|
|
|
input_details = interpreter.get_input_details() |
|
output_details = interpreter.get_output_details() |
|
input_height, input_width, _ = input_details[0]['shape'][1:] |
|
float_input = (input_details[0]['dtype'] == np.float32) |
|
input_mean = 127.5 |
|
input_std = 127.5 |
|
|
|
|
|
|
|
|
|
cap = cv2.VideoCapture(0) |
|
cap.set(cv2.CAP_PROP_BUFFERSIZE, 1) |
|
frame_rate = 20 |
|
cap.set(cv2.CAP_PROP_FPS, frame_rate) |
|
|
|
frame_counter = 0 |
|
|
|
while True: |
|
|
|
ret, frame = cap.read() |
|
|
|
|
|
if not ret: |
|
print("Error reading frame from the webcam") |
|
break |
|
|
|
frame_counter += 2 |
|
|
|
|
|
if frame_counter % frame_skip != 0: |
|
continue |
|
|
|
|
|
input_data = cv2.resize(frame, (input_width, input_height)) |
|
|
|
|
|
if input_data.size == 0: |
|
print("Error resizing the frame") |
|
break |
|
|
|
input_data = np.expand_dims(input_data, axis=0) |
|
|
|
|
|
if float_input: |
|
input_data = (np.float32(input_data) - input_mean) / input_std |
|
|
|
|
|
interpreter.set_tensor(input_details[0]['index'], input_data) |
|
interpreter.invoke() |
|
|
|
|
|
boxes = interpreter.get_tensor(output_details[1]['index'])[0] |
|
classes = interpreter.get_tensor(output_details[3]['index'])[0] |
|
scores = interpreter.get_tensor(output_details[0]['index'])[0] |
|
|
|
for i in range(len(scores)): |
|
if 0.0 < scores[i] <= 1.0 and scores[i] > min_conf: |
|
ymin, xmin, ymax, xmax = boxes[i] |
|
ymin, xmin, ymax, xmax = ( |
|
int(ymin * frame.shape[0]), |
|
int(xmin * frame.shape[1]), |
|
int(ymax * frame.shape[0]), |
|
int(xmax * frame.shape[1]), |
|
) |
|
|
|
|
|
|
|
|
|
|
|
object_name = labels[int(classes[i])] |
|
label = f'{object_name}: {int(scores[i] * 100)}%' |
|
label_ymin = max(ymin+26, 10) |
|
|
|
cv2.putText(frame, label, (xmin, label_ymin), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) |
|
|
|
corner_thickness = 6 |
|
corner_offset = 10 |
|
corner_colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0)] |
|
|
|
cv2.line(frame, (xmin, ymin), (xmin + corner_offset, ymin), corner_colors[1], thickness=corner_thickness) |
|
cv2.line(frame, (xmin, ymin), (xmin, ymin + corner_offset), corner_colors[1], thickness=corner_thickness) |
|
|
|
|
|
cv2.line(frame, (xmax, ymin), (xmax - corner_offset, ymin), corner_colors[1], thickness=corner_thickness) |
|
cv2.line(frame, (xmax, ymin), (xmax, ymin + corner_offset), corner_colors[1], thickness=corner_thickness) |
|
|
|
|
|
cv2.line(frame, (xmin, ymax), (xmin + corner_offset, ymax), corner_colors[1], thickness=corner_thickness) |
|
cv2.line(frame, (xmin, ymax), (xmin, ymax - corner_offset), corner_colors[1], thickness=corner_thickness) |
|
|
|
|
|
cv2.line(frame, (xmax, ymax), (xmax - corner_offset, ymax), corner_colors[1], thickness=corner_thickness) |
|
cv2.line(frame, (xmax, ymax), (xmax, ymax - corner_offset), corner_colors[1], thickness=corner_thickness) |
|
|
|
|
|
|
|
cv2.imshow("Object Detection", frame) |
|
|
|
|
|
if cv2.waitKey(1) & 0xFF == ord('q'): |
|
break |
|
|
|
|
|
cap.release() |
|
cv2.destroyAllWindows() |
|
|
|
|
|
model_path = '/Users/macmini/Desktop/pptflite/detect.tflite' |
|
label_path = '/Users/macmini/Desktop/pptflite/labelmap.txt' |
|
tflite_detect_webcam(model_path, label_path, min_conf=0.5, frame_skip=5) |
|
|