diff --git a/assets/cat_dog.jpg b/assets/cat_dog.jpg new file mode 100644 index 0000000..177840b Binary files /dev/null and b/assets/cat_dog.jpg differ diff --git a/assets/girl_faces.png b/assets/girl_faces.png new file mode 100644 index 0000000..ae0428d Binary files /dev/null and b/assets/girl_faces.png differ diff --git a/projects/CV/.gitignore b/projects/CV/.gitignore new file mode 100644 index 0000000..0da9986 --- /dev/null +++ b/projects/CV/.gitignore @@ -0,0 +1,3 @@ +test/ +capture.png +yolov8*.pt diff --git a/projects/CV/Assets/cat_dog.jpg b/projects/CV/Assets/cat_dog.jpg new file mode 120000 index 0000000..68dea0f --- /dev/null +++ b/projects/CV/Assets/cat_dog.jpg @@ -0,0 +1 @@ +../../../assets/cat_dog.jpg \ No newline at end of file diff --git a/projects/CV/Assets/girl_faces.png b/projects/CV/Assets/girl_faces.png new file mode 120000 index 0000000..0fb842a --- /dev/null +++ b/projects/CV/Assets/girl_faces.png @@ -0,0 +1 @@ +../../../assets/girl_faces.png \ No newline at end of file diff --git a/projects/CV/DetectFaces/__main__.py b/projects/CV/DetectFaces/__main__.py new file mode 100644 index 0000000..70fe275 --- /dev/null +++ b/projects/CV/DetectFaces/__main__.py @@ -0,0 +1,33 @@ +import cv2 + +image_path = './Assets/girl_faces.png' +face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') + + +def viewImage(image, name_of_window): + cv2.namedWindow(name_of_window, cv2.WINDOW_NORMAL) + cv2.imshow(name_of_window, image) + cv2.waitKey(0) + cv2.destroyAllWindows() + + +if __name__ == '__main__': + image = cv2.imread(image_path) + + gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) + + faces = face_cascade.detectMultiScale( + gray, + scaleFactor=1.1, + minNeighbors=5, + minSize=(10, 10), + ) + + faces_detected_string = 'Лиц обнаружено: ' + format(len(faces)) + print(faces_detected_string) + + # Рисуем квадраты вокруг лиц + for x, y, w, h in faces: + cv2.rectangle(image, (x, y), (x + w, y + h), (255, 255, 0), 2) + + viewImage(image, faces_detected_string) diff --git a/projects/CV/README.md b/projects/CV/README.md new file mode 100644 index 0000000..74904f5 --- /dev/null +++ b/projects/CV/README.md @@ -0,0 +1,6 @@ +Python модули +------------- + +* `VideoCapture` — модуль для проверки захвата RTSP потока +* `DetectFaces` — пример распознования лиц на основе каскада Хаара +* `Stream` - пример с сайта `ultralytics` распозования на видео diff --git a/projects/CV/Stream/__main__.py b/projects/CV/Stream/__main__.py new file mode 100644 index 0000000..f657194 --- /dev/null +++ b/projects/CV/Stream/__main__.py @@ -0,0 +1,37 @@ +import cv2 +from ultralytics import YOLO + +# https://docs.ultralytics.com/modes/predict/#streaming-source-for-loop + +# Load the YOLOv8 model +model = YOLO('yolov8m.pt') + +# Open the video file +video_path = 'run.mp4' +cap = cv2.VideoCapture(video_path) + +# Loop through the video frames +while cap.isOpened(): + # Read a frame from the video + success, frame = cap.read() + + if success: + # Run YOLOv8 inference on the frame + results = model(frame) + + # Visualize the results on the frame + annotated_frame = results[0].plot() + + # Display the annotated frame + cv2.imshow('YOLOv8 Inference', annotated_frame) + + # Break the loop if 'q' is pressed + if cv2.waitKey(1) & 0xFF == ord('q'): + break + else: + # Break the loop if the end of the video is reached + break + +# Release the video capture object and close the display window +cap.release() +cv2.destroyAllWindows() diff --git a/projects/CV/VideoCapture/__main__.py b/projects/CV/VideoCapture/__main__.py new file mode 100644 index 0000000..8707466 --- /dev/null +++ b/projects/CV/VideoCapture/__main__.py @@ -0,0 +1,21 @@ +import cv2 + +RTSP_ADDR = "rtsp://localhost:8554/cam" + +cam = cv2.VideoCapture(RTSP_ADDR) + +while True: + result, image = cam.read() + + cv2.imshow("Camera", image) + + if cv2.waitKey(1) & 0xFF == ord("s"): + cv2.imwrite("capture.png", image) + + if cv2.waitKey(1) & 0xFF == ord("q"): + break + +cam.release() + +cv2.destroyWindow("Camera") +cv2.destroyAllWindows() diff --git a/projects/CV/ex.py b/projects/CV/ex.py new file mode 100644 index 0000000..9a656bf --- /dev/null +++ b/projects/CV/ex.py @@ -0,0 +1,28 @@ +from ultralytics import YOLO + +model = YOLO('yolov8m.pt') +model.save_dir = './predict/' + +result = model.predict( + './Assets/cat_dog.jpg', + save=True, + device='cpu', + show=False, + classes=[16, 15], +)[0] +result = model( + './Assets/cat_dog.jpg', + save=True, + project='test', + name='ololo', + device='cpu', + show=False, +)[0] + +# print(len(result.boxes)) + +# for _ in result.boxes: +# box = result.boxes[_] +# print("Object type:", box.cls) +# print("Coordinates:", box.xyxy) +# print("Probability:", box.conf) diff --git a/projects/CV/haarcascade_frontalface_default.xml b/projects/CV/haarcascade_frontalface_default.xml new file mode 120000 index 0000000..55b63b6 --- /dev/null +++ b/projects/CV/haarcascade_frontalface_default.xml @@ -0,0 +1 @@ +/usr/share/opencv4/haarcascades/haarcascade_frontalface_default.xml \ No newline at end of file diff --git a/projects/CV/pyproject.toml b/projects/CV/pyproject.toml new file mode 100644 index 0000000..cab6ef7 --- /dev/null +++ b/projects/CV/pyproject.toml @@ -0,0 +1,4 @@ +[tool.black] +skip-string-normalization = true +pycodestyle = true +line-length = 100 diff --git a/projects/CV/requirements.txt b/projects/CV/requirements.txt new file mode 100644 index 0000000..7e18ec5 --- /dev/null +++ b/projects/CV/requirements.txt @@ -0,0 +1,2 @@ +notebook==7.0.4 +ultralytics==8.0.188