-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from AlanWang611/ultralytics
Ultralytics
- Loading branch information
Showing
13 changed files
with
392 additions
and
82 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from ultralytics import YOLO | ||
from PIL import Image | ||
from http.server import BaseHTTPRequestHandler, HTTPServer | ||
import io | ||
import cv2 | ||
import time | ||
import requests | ||
|
||
|
||
image_count = 0 | ||
model = YOLO('best_trash.pt') | ||
|
||
def predict_image(image_path): | ||
|
||
# model.predict(image_path, save=True, imgsz=1280, conf=0.25, show_labels=True, show_conf=True, iou=0.5, | ||
# line_width=3) | ||
|
||
#plot stuff onto image | ||
im = cv2.imread(image_path) | ||
result = model(im, imgsz=1280, conf=0.15, show_labels=True, show_conf=True, iou=0.5, line_width=3) | ||
annotated_frame = result[0].plot() | ||
# cv2.imshow('Result', annotated_frame) | ||
# if cv2.waitKey(1) & 0xFF==ord("q"): | ||
# return | ||
# cv2.destroyAllWindows() | ||
# cv2.waitKey(0) | ||
|
||
|
||
# result boxes: all coords: x1, y1, x2, y2 | ||
# result_boxes = result[0].boxes.xyxy.cpu().detach().numpy() | ||
return result[0].tojson() | ||
|
||
|
||
class HTTPRequestHandler(BaseHTTPRequestHandler): | ||
|
||
# POST method handler | ||
def do_POST(self): | ||
print("Got post") | ||
global image_count | ||
content_length = int(self.headers['Content-Length']) | ||
post_data = self.rfile.read(content_length) | ||
image_filename = 'images/' + str(image_count) + '.jpg' | ||
image_count += 1 | ||
with open(image_filename, 'wb') as f: | ||
f.write(post_data) | ||
|
||
self.send_response(200) | ||
self.send_header('Content-type', 'application/json') | ||
self.end_headers() | ||
prediction = predict_image(image_filename) | ||
print(prediction) | ||
self.wfile.write(prediction.encode('utf-8')) | ||
|
||
|
||
def main(): | ||
server_address = ('', 8001) | ||
httpd = HTTPServer(server_address, HTTPRequestHandler) | ||
print('start server') | ||
httpd.serve_forever() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import cv2, shutil | ||
|
||
def capture_image(): | ||
# Open the camera | ||
cap = cv2.VideoCapture(0) | ||
|
||
# Check if the camera is opened successfully | ||
if not cap.isOpened(): | ||
print("Error: Unable to open camera.") | ||
return | ||
|
||
# Capture a frame | ||
ret, frame = cap.read() | ||
|
||
if ret: | ||
# Save the captured frame as an image | ||
cv2.imwrite("frame.jpg", frame) | ||
# Send frame to trash_detection.py | ||
shutil.copy("frame.jpg", "trash_detection.pg") | ||
else: | ||
print("Error: Unable to capture image.") | ||
|
||
# Release the camera | ||
cap.release() | ||
|
||
robot_on = True | ||
while robot_on: | ||
capture_image() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Image Fetch</title> | ||
<style> | ||
body, html { | ||
margin: 0; | ||
padding: 0; | ||
width: 100%; | ||
height: 100%; | ||
overflow: hidden; | ||
} | ||
img { | ||
max-width: 100%; | ||
max-height: 100%; | ||
display: block; | ||
margin: auto; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<img id="dynamicImage" src="" alt="Dynamic Image"> | ||
<script> | ||
function fetchImage() { | ||
const imageElement = document.getElementById('dynamicImage'); | ||
// Update the source with a query string to avoid caching issues | ||
imageElement.src = 'http://localhost:8000/image.jpg?' + new Date().getTime(); | ||
} | ||
|
||
// Fetch an image every second | ||
setInterval(fetchImage, 1000); | ||
|
||
// Fetch the first image immediately on load | ||
window.onload = fetchImage; | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from http.server import BaseHTTPRequestHandler, HTTPServer | ||
import os | ||
from pathlib import Path | ||
|
||
|
||
class HTTPRequestHandler(BaseHTTPRequestHandler): | ||
|
||
# POST method handler | ||
def do_GET(self): | ||
directory = '../computer_vision/images/' # Adjust the path to your image directory | ||
|
||
try: | ||
# Get all image files in the directory | ||
files = list(Path(directory).glob('*')) | ||
# Filter files to only get those that are valid images | ||
image_files = [file for file in files if file.suffix.lower() in ['.jpg', '.jpeg', '.png', '.gif', '.bmp']] | ||
print(image_files) | ||
|
||
# Find the oldest file based on creation time | ||
oldest_file = max(image_files, key=os.path.getctime, default=None) | ||
|
||
if oldest_file is None: | ||
self.send_error(404, "No image files found.") | ||
return | ||
|
||
print(oldest_file) | ||
|
||
# Open the oldest image file and send it | ||
with open(oldest_file, 'rb') as file: | ||
self.send_response(200) | ||
self.send_header('Content-type', 'image/jpeg') # Change if using different image types | ||
self.end_headers() | ||
self.wfile.write(file.read()) | ||
except Exception as e: | ||
self.send_error(500, f"Server Error: {e}") | ||
|
||
|
||
def run(server_class=HTTPServer, handler_class=HTTPRequestHandler, port=8000): | ||
server_address = ('', port) | ||
httpd = server_class(server_address, handler_class) | ||
print(f"Server starting on port {port}...") | ||
httpd.serve_forever() | ||
|
||
|
||
if __name__ == "__main__": | ||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import math | ||
|
||
|
||
CAMERA_FOV_RADIANS = (52.31 * math.pi / 180, 51.83 * math.pi / 180) | ||
CAMERA_FOV_RADIANS = (68.61668666 * math.pi / 180, 65.68702522 * math.pi / 180) # horizontal, vertical | ||
CAMERA_DIMENSIONS_PIXELS = (640, 480) | ||
CAMERA_HEIGHT_ABOVE_GROUND_METERS = 0.263 | ||
CAMERA_HEIGHT_ABOVE_GROUND_METERS = .084 |
Oops, something went wrong.