Compare commits
2 Commits
bc92df3242
...
b99433e992
Author | SHA1 | Date | |
---|---|---|---|
|
b99433e992 | ||
|
a9aefef662 |
58
car-detection/main.py
Normal file
58
car-detection/main.py
Normal file
@ -0,0 +1,58 @@
|
||||
import cv2
|
||||
import os
|
||||
from ultralytics import YOLO
|
||||
from datetime import datetime
|
||||
|
||||
# Load a YOLOv8 pretrained model (use 'yolov8n.pt' or 'yolov8s.pt' for speed)
|
||||
model = YOLO('yolov8n.pt')
|
||||
|
||||
# Create directory to save car images
|
||||
save_dir = 'captured_cars'
|
||||
os.makedirs(save_dir, exist_ok=True)
|
||||
|
||||
# Open webcam (0 = default camera)
|
||||
cap = cv2.VideoCapture(0)
|
||||
|
||||
if not cap.isOpened():
|
||||
print("Failed to open webcam.")
|
||||
exit()
|
||||
|
||||
print("Press Q to quit.")
|
||||
|
||||
while True:
|
||||
ret, frame = cap.read()
|
||||
if not ret:
|
||||
break
|
||||
|
||||
# Run YOLO inference
|
||||
results = model(frame)
|
||||
|
||||
for result in results:
|
||||
for box in result.boxes:
|
||||
cls_id = int(box.cls[0])
|
||||
conf = float(box.conf[0])
|
||||
|
||||
# Class 2 = car in COCO dataset
|
||||
if cls_id == 2 and conf > 0.5:
|
||||
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
||||
car_crop = frame[y1:y2, x1:x2]
|
||||
|
||||
# Save image
|
||||
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S_%f')
|
||||
filename = os.path.join(save_dir, f'car_{timestamp}.jpg')
|
||||
cv2.imwrite(filename, car_crop)
|
||||
print(f"Saved: {filename}")
|
||||
|
||||
# Draw bounding box
|
||||
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
||||
cv2.putText(frame, f"Car {conf:.2f}", (x1, y1 - 10),
|
||||
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
|
||||
|
||||
# Show the frame
|
||||
cv2.imshow("Car Detection", frame)
|
||||
|
||||
if cv2.waitKey(1) & 0xFF == ord('q'):
|
||||
break
|
||||
|
||||
cap.release()
|
||||
cv2.destroyAllWindows()
|
98
circle-trace-thing/main.py
Normal file
98
circle-trace-thing/main.py
Normal file
@ -0,0 +1,98 @@
|
||||
import pygame
|
||||
import math
|
||||
import sys
|
||||
|
||||
pygame.init()
|
||||
WIDTH, HEIGHT = 800, 600
|
||||
screen = pygame.display.set_mode((WIDTH, HEIGHT))
|
||||
pygame.display.set_caption("Ray Marching - Single Ray with Steps")
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
# Colors
|
||||
BLACK = (0, 0, 0)
|
||||
WHITE = (255, 255, 255)
|
||||
GRAY = (180, 180, 180)
|
||||
RED = (255, 0, 0)
|
||||
GREEN = (0, 255, 0)
|
||||
BLUE = (0, 0, 255)
|
||||
|
||||
# SDF primitives
|
||||
def sdf_circle(p, center, radius):
|
||||
return math.hypot(p[0] - center[0], p[1] - center[1]) - radius
|
||||
|
||||
def sdf_box(p, center, size):
|
||||
dx = abs(p[0] - center[0]) - size[0] / 2
|
||||
dy = abs(p[1] - center[1]) - size[1] / 2
|
||||
dx = max(dx, 0)
|
||||
dy = max(dy, 0)
|
||||
return math.hypot(dx, dy)
|
||||
|
||||
objects = [
|
||||
{"sdf": lambda p: sdf_circle(p, (400, 300), 60), "color": GREEN},
|
||||
{"sdf": lambda p: sdf_box(p, (200, 150), (100, 100)), "color": BLUE},
|
||||
{"sdf": lambda p: sdf_box(p, (600, 400), (80, 150)), "color": RED},
|
||||
]
|
||||
|
||||
def scene_sdf(p):
|
||||
return min(obj["sdf"](p) for obj in objects)
|
||||
|
||||
def ray_march(origin, direction, max_steps=100, max_dist=1000, epsilon=1.0):
|
||||
p = list(origin)
|
||||
total_dist = 0
|
||||
steps = []
|
||||
for _ in range(max_steps):
|
||||
dist = scene_sdf(p)
|
||||
steps.append((tuple(p), dist))
|
||||
if dist < epsilon or total_dist > max_dist:
|
||||
break
|
||||
p[0] += direction[0] * dist
|
||||
p[1] += direction[1] * dist
|
||||
total_dist += dist
|
||||
return steps
|
||||
|
||||
# Main loop
|
||||
running = True
|
||||
while running:
|
||||
screen.fill(BLACK)
|
||||
mouse_pos = pygame.mouse.get_pos()
|
||||
origin = (600, 200)
|
||||
dx = mouse_pos[0] - origin[0]
|
||||
dy = mouse_pos[1] - origin[1]
|
||||
length = math.hypot(dx, dy)
|
||||
if length == 0:
|
||||
direction = (0, 0)
|
||||
else:
|
||||
direction = (dx / length, dy / length)
|
||||
|
||||
# Draw scene objects
|
||||
for obj in objects:
|
||||
if obj["color"] == GREEN:
|
||||
pygame.draw.circle(screen, obj["color"], (400, 300), 60, 2)
|
||||
elif obj["color"] == BLUE:
|
||||
pygame.draw.rect(screen, obj["color"], pygame.Rect(150, 100, 100, 100), 2)
|
||||
elif obj["color"] == RED:
|
||||
pygame.draw.rect(screen, obj["color"], pygame.Rect(560, 325, 80, 150), 2)
|
||||
|
||||
# Perform ray march
|
||||
steps = ray_march(origin, direction)
|
||||
|
||||
# Draw ray steps
|
||||
for point, dist in steps:
|
||||
pygame.draw.circle(screen, GRAY, (int(point[0]), int(point[1])), int(dist), 1)
|
||||
|
||||
# Draw final point
|
||||
if steps:
|
||||
pygame.draw.circle(screen, WHITE, (int(steps[-1][0][0]), int(steps[-1][0][1])), 3)
|
||||
|
||||
pygame.draw.line(screen, WHITE, origin, mouse_pos, 1)
|
||||
pygame.draw.circle(screen, WHITE, origin, 4)
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
running = False
|
||||
|
||||
pygame.display.flip()
|
||||
clock.tick(60)
|
||||
|
||||
pygame.quit()
|
||||
sys.exit()
|
@ -140,8 +140,8 @@ while running:
|
||||
if ray_y >= seafloor[int(ray_x)]:
|
||||
blips.append((sonar_angle, i, now))
|
||||
break
|
||||
|
||||
elif sonar_mode == "passive":
|
||||
# Passive sonar "listens" and picks up nearby terrain noise randomly
|
||||
for _ in range(2):
|
||||
angle = random.uniform(0, 360)
|
||||
distance = random.randint(30, sonar_radius)
|
||||
@ -185,8 +185,12 @@ while running:
|
||||
screen.blit(surf, (bx - 2, by - 2))
|
||||
end_x = sonar_center[0] + math.cos(sweep_radians) * sonar_radius
|
||||
end_y = sonar_center[1] + math.sin(sweep_radians) * sonar_radius
|
||||
if sonar_mode == "active":
|
||||
pygame.draw.line(screen, CYAN, sonar_center, (end_x, end_y), 2)
|
||||
pygame.draw.circle(screen, CYAN, sonar_center, 3)
|
||||
|
||||
else:
|
||||
pygame.draw.circle(screen, CYAN, sonar_center, 1)
|
||||
mode_text = font.render(f"Sonar: {sonar_mode.upper()} (TAB)", True, WHITE)
|
||||
screen.blit(mode_text, (sonar_center[0] - 60, sonar_center[1] + sonar_radius + 10))
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user