This commit is contained in:
Alexander Popov 2023-09-29 21:51:53 +03:00
parent f8b9ae2443
commit cde52c4596
Signed by: iiiypuk
GPG Key ID: E47FE0AB36CD5ED6
13 changed files with 137 additions and 0 deletions

BIN
assets/cat_dog.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

BIN
assets/girl_faces.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 334 KiB

3
projects/CV/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
test/
capture.png
yolov8*.pt

View File

@ -0,0 +1 @@
../../../assets/cat_dog.jpg

View File

@ -0,0 +1 @@
../../../assets/girl_faces.png

View File

@ -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)

6
projects/CV/README.md Normal file
View File

@ -0,0 +1,6 @@
Python модули
-------------
* `VideoCapture` — модуль для проверки захвата RTSP потока
* `DetectFaces` — пример распознования лиц на основе каскада Хаара
* `Stream` - пример с сайта `ultralytics` распозования на видео

View File

@ -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()

View File

@ -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()

28
projects/CV/ex.py Normal file
View File

@ -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)

View File

@ -0,0 +1 @@
/usr/share/opencv4/haarcascades/haarcascade_frontalface_default.xml

View File

@ -0,0 +1,4 @@
[tool.black]
skip-string-normalization = true
pycodestyle = true
line-length = 100

View File

@ -0,0 +1,2 @@
notebook==7.0.4
ultralytics==8.0.188