feat: Add webcam support
Esse commit está contido em:
@@ -286,3 +286,48 @@ def detect_video(yolo, video_path, output_path=""):
|
||||
vid.release()
|
||||
out.release()
|
||||
# yolo.close_session()
|
||||
|
||||
|
||||
# TO PROCESS VIDEOS DIRECTLY FROM WEBCAM
|
||||
def detect_webcam(yolo):
|
||||
import cv2
|
||||
|
||||
vid = cv2.VideoCapture(0)
|
||||
if not vid.isOpened():
|
||||
raise IOError("Couldn't open webcam")
|
||||
accum_time = 0
|
||||
curr_fps = 0
|
||||
fps = "FPS: ??"
|
||||
prev_time = timer()
|
||||
while vid.isOpened():
|
||||
return_value, frame = vid.read()
|
||||
if not return_value:
|
||||
break
|
||||
image = Image.fromarray(frame)
|
||||
out_pred, image = yolo.detect_image(image, show_stats=False)
|
||||
result = np.asarray(image)
|
||||
curr_time = timer()
|
||||
exec_time = curr_time - prev_time
|
||||
prev_time = curr_time
|
||||
accum_time = accum_time + exec_time
|
||||
curr_fps = curr_fps + 1
|
||||
if accum_time > 1:
|
||||
accum_time = accum_time - 1
|
||||
fps = "FPS: " + str(curr_fps)
|
||||
curr_fps = 0
|
||||
cv2.putText(
|
||||
result,
|
||||
text=fps,
|
||||
org=(3, 15),
|
||||
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
|
||||
fontScale=0.50,
|
||||
color=(255, 0, 0),
|
||||
thickness=2,
|
||||
)
|
||||
cv2.namedWindow("Result", cv2.WINDOW_NORMAL)
|
||||
cv2.imshow("Result", result)
|
||||
cv2.waitKey(1)
|
||||
if cv2.getWindowProperty("Result", cv2.WND_PROP_VISIBLE) < 1:
|
||||
break
|
||||
vid.release()
|
||||
yolo.close_session()
|
||||
|
||||
@@ -18,7 +18,7 @@ sys.path.append(src_path)
|
||||
sys.path.append(utils_path)
|
||||
|
||||
import argparse
|
||||
from keras_yolo3.yolo import YOLO, detect_video
|
||||
from keras_yolo3.yolo import YOLO, detect_video, detect_webcam
|
||||
from PIL import Image
|
||||
from timeit import default_timer as timer
|
||||
from utils import load_extractor_model, load_features, parse_input, detect_object
|
||||
@@ -149,12 +149,21 @@ if __name__ == "__main__":
|
||||
help="Use the tiny Yolo version for better performance and less accuracy. Default is False.",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--webcam",
|
||||
default=False,
|
||||
action="store_true",
|
||||
help="Use webcam for real-time detection. Default is False.",
|
||||
)
|
||||
|
||||
FLAGS = parser.parse_args()
|
||||
|
||||
save_img = not FLAGS.no_save_img
|
||||
|
||||
file_types = FLAGS.file_types
|
||||
|
||||
webcam_active = FLAGS.webcam
|
||||
|
||||
if file_types:
|
||||
input_paths = GetFileList(FLAGS.input_path, endings=file_types)
|
||||
else:
|
||||
@@ -215,7 +224,7 @@ if __name__ == "__main__":
|
||||
input_labels = [line.rstrip("\n") for line in class_file.readlines()]
|
||||
print("Found {} input labels: {} ...".format(len(input_labels), input_labels))
|
||||
|
||||
if input_image_paths:
|
||||
if input_image_paths and not webcam_active:
|
||||
print(
|
||||
"Found {} input images: {} ...".format(
|
||||
len(input_image_paths),
|
||||
@@ -272,7 +281,8 @@ if __name__ == "__main__":
|
||||
out_df.to_csv(FLAGS.box, index=False)
|
||||
|
||||
# This is for videos
|
||||
if input_video_paths:
|
||||
# for pre-recorded videos present in the Test_Images folder
|
||||
if input_video_paths and not webcam_active:
|
||||
print(
|
||||
"Found {} input videos: {} ...".format(
|
||||
len(input_video_paths),
|
||||
@@ -293,5 +303,12 @@ if __name__ == "__main__":
|
||||
len(input_video_paths), end - start
|
||||
)
|
||||
)
|
||||
# for Webcam
|
||||
if webcam_active:
|
||||
start = timer()
|
||||
detect_webcam(yolo)
|
||||
end = timer()
|
||||
print("Processed from webcam for {:.1f}sec".format(end - start))
|
||||
|
||||
# Close the current yolo session
|
||||
yolo.close_session()
|
||||
|
||||
@@ -6,7 +6,7 @@ To detect objects run the detector script from within the [`TrainYourOwnYOLO/3_I
|
||||
```
|
||||
python Detector.py
|
||||
```
|
||||
The outputs are saved to [`TrainYourOwnYOLO/Data/Source_Images/Test_Image_Detection_Results`](/Data/Source_Images/Test_Image_Detection_Results). The outputs include the original images with bounding boxes and confidence scores as well as a file called [`Detection_Results.csv`](/Data/Source_Images/Test_Image_Detection_Results/Detection_Results.csv) containing the image file paths and the bounding box coordinates. For videos, the output files are videos with bounding boxes and confidence scores. To list available command line options run `python Detector.py -h`.
|
||||
The outputs are saved to [`TrainYourOwnYOLO/Data/Source_Images/Test_Image_Detection_Results`](/Data/Source_Images/Test_Image_Detection_Results). The outputs include the original images with bounding boxes and confidence scores as well as a file called [`Detection_Results.csv`](/Data/Source_Images/Test_Image_Detection_Results/Detection_Results.csv) containing the image file paths and the bounding box coordinates. For videos, the output files are videos with bounding boxes and confidence scores. For real-time detection use `python Detector.py --webcam` this will open up your webcam and detect your data classes. To list available command line options run `python Detector.py -h`.
|
||||
|
||||
### That's all!
|
||||
Congratulations on building your own custom YOLOv3 computer vision application.
|
||||
|
||||
Referência em uma Nova Issue
Bloquear um usuário