Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Machine Learning algorithm completed #15

Merged
merged 2 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions ImageRecognition/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Technology
<p align="center">
<a href="https://www.ultralytics.com" target="blank"><img src="https://assets-global.website-files.com/646dd1f1a3703e451ba81ecc/64777c3e071ec953437e6950_logo.svg" alt="Ultralytics Yolo" /></a>
</p>

Ultralytics YOLO for building accurate and efficient object detection models.

## Running the app
First, open the terminal and move to the ImageRecognition directiory.
Run send_frame.py for sending camera frames:

```bash
python send_frame.py
```

To start the object detection function move to the src directory and run ObjectDetectionYOLO.py:

```bash
python ObjectDetectionYOLO.py
```

## Learn More
To learn more about YOLO architecture, take a look at the following resource:
- [YOLO Documentation](https://docs.ultralytics.com).
3 changes: 2 additions & 1 deletion ImageRecognition/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
opencv-python~=4.8.1.78
numpy~=1.26.1
ping3~=4.0.4
ffmpeg-python~=0.2.0
python>=3.8
PyTorch>=1.8
3 changes: 1 addition & 2 deletions ImageRecognition/send_frame.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import cv2
import socketio
import base64
import threading

#cameras data
Expand Down Expand Up @@ -40,7 +39,7 @@ def send_frames(camera_id, capture, interval=0.1):
for camera_id, camera_url in cameras.items():
capture = cv2.VideoCapture(camera_url)
if not capture.isOpened():
print(f"Errore nell'apertura della telecamera {camera_id}")
print(f"Error stream with camera {camera_id}")
continue

threading.Thread(target=send_frames, args=(camera_id, capture)).start()
Expand Down
28 changes: 15 additions & 13 deletions ImageRecognition/src/ObjectDetectionYOLO.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,21 @@
cam8 = 'rtsp://192.168.1.41:80/ch7_0.264'


TOKEN = '6929905186:AAEouI3G2sbfS-y6ZzkXrpNgPQRAPs5_v-g'
channel_id = '792557360'
last_sent_image = None

def post_request(image):
def post_request(image, camera_id, status):
global last_sent_image
apiURL = f"https://api.telegram.org/bot{TOKEN}/sendPhoto?chat_id={channel_id}"
url = 'http://localhost:8080'

data = {
'id': camera_id,
'status': status,
}

if last_sent_image is None:
last_sent_image = image
try:
response = requests.post(apiURL, files={'photo': image})
response = requests.post(url, data=data, files={'file': (image, 'image/jpeg')})
print("Status code is: ", response.status_code)
except requests.exceptions.RequestException as e:
print("There was an exception that occurred while handling your request.", e)
Expand All @@ -59,7 +62,7 @@ def post_request(image):
print("MEAN SQUARED ERROR is: ", mse)
if mse > 105.85:
try:
response = requests.post(apiURL, files={'photo': image})
response = requests.post(url, data=data, files={'file': (image, 'image/jpeg')})
print("Status code is: ", response.status_code)

if response.status_code == 200:
Expand All @@ -73,16 +76,17 @@ def post_request(image):
#filename = url of cam
#file_index = index of the file that can be assigned to each thread. cam1 has file_index as 1, cam2 has file_index as 2...
def detection(filename, file_index):
cap = cv2.VideoCapture(filename) # Read the video file
cap = cv2.VideoCapture(filename)
global last_sent_image
status = 'offline'

while cap.isOpened():

success, img = cap.read()
# Exit the loop if no more frames in either video
if not success:
break

status = 'online'
results = model(img, stream = True, classes = 0, conf=0.5)
foundPerson = False
#coordinates
Expand All @@ -91,8 +95,7 @@ def detection(filename, file_index):
for box in boxes:
#bounding box
x1, y1, x2, y2 = box.xyxy[0]
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) # convert to int values
# put box in cam
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 255), 3)
# confidence
confidence = math.ceil((box.conf[0]*100))/100
Expand All @@ -105,14 +108,13 @@ def detection(filename, file_index):

if(foundPerson):
_, img_encoded = cv2.imencode('.jpg', img)
post_request(img_encoded)
post_request(img_encoded, file_index, status)


cap.release()



#initialize thread
detect_thread1 = threading.Thread(target=detection,
args=(cam1, 1),
daemon=True)
Expand Down Expand Up @@ -145,7 +147,7 @@ def detection(filename, file_index):
args=(cam8, 8),
daemon=True)

#start thread

detect_thread1.start()
detect_thread2.start()
detect_thread3.start()
Expand Down
Loading