-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
06e42cb
commit 47eaf3d
Showing
30 changed files
with
537 additions
and
0 deletions.
There are no files selected for viewing
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 @@ | ||
*.pt |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
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,25 @@ | ||
def vector_cross_product(v1, v2): | ||
return v1[0] * v2[1] - v1[1] * v2[0] | ||
|
||
def check_same_side(A, B, C, D): | ||
AB = (B[0] - A[0], B[1] - A[1]) | ||
AC = (C[0] - A[0], C[1] - A[1]) | ||
AD = (D[0] - A[0], D[1] - A[1]) | ||
|
||
cross1 = vector_cross_product(AB, AC) | ||
cross2 = vector_cross_product(AB, AD) | ||
|
||
if cross1 * cross2 > 0: | ||
return True # Cùng phía | ||
else: | ||
return False # Khác phía | ||
|
||
A = (1, 1) | ||
B = (4, 4) | ||
C = (2, 3) | ||
D = (1, 1) | ||
|
||
if check_same_side(A, B, C, D): | ||
print("Điểm D nằm cùng phía với điểm C so với đường thẳng AB.") | ||
else: | ||
print("Điểm D nằm khác phía với điểm C so với đường thẳng AB.") |
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,21 @@ | ||
from datetime import datetime | ||
|
||
|
||
def convert_to_timestamp(start, end): | ||
# time_select = '2024-05-06 08:00' | ||
|
||
start_dt_object = datetime.strptime(start, '%Y-%m-%d %H:%M') | ||
st_timestamp = int(start_dt_object.timestamp() * 1000) | ||
|
||
end_dt_object = datetime.strptime(end, '%Y-%m-%d %H:%M') | ||
end_timestamp = int(end_dt_object.timestamp() * 1000) | ||
return st_timestamp, end_timestamp | ||
|
||
|
||
start = '2024-05-04 08:00' | ||
end = '2024-05-04 10:00' | ||
st_timestamp, end_timestamp = convert_to_timestamp(start, end) | ||
|
||
print("start_time:", st_timestamp) | ||
print("end_time:", end_timestamp) | ||
|
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,88 @@ | ||
import cv2 | ||
import numpy as np | ||
from ultralytics import YOLO | ||
import time | ||
from ultralytics.utils.checks import check_imshow | ||
from ultralytics.utils.plotting import Annotator, colors | ||
|
||
from collections import defaultdict | ||
|
||
track_history = defaultdict(lambda: []) | ||
model = YOLO("yolov8x.pt") | ||
names = model.model.names | ||
|
||
|
||
video_path = "input_video/market_square_720_1280.mp4" | ||
cap = cv2.VideoCapture(video_path) | ||
assert cap.isOpened(), "Error reading video file" | ||
|
||
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS)) | ||
print(w,h) | ||
|
||
resize = (720, 1280) | ||
# resize = (1280, 720) | ||
|
||
output = 'output_video/market_square_720_1280.mp4' | ||
fourcc = cv2.VideoWriter_fourcc(*'mp4v') | ||
out = cv2.VideoWriter(output, fourcc, 40, resize, True) | ||
|
||
prev_frame_time = 0 | ||
new_frame_time = 0 | ||
|
||
while cap.isOpened(): | ||
success, frame = cap.read() | ||
if success: | ||
# frame = cv2.resize(frame, resize) | ||
results = model.track(frame, persist=True, verbose=False, tracker='bytetrack.yaml') | ||
boxes = results[0].boxes.xyxy.cpu() | ||
annotated_frame = results[0].plot() | ||
frame = annotated_frame | ||
annotator = Annotator(frame, line_width=2) | ||
red_color = (0, 0, 255) | ||
# if results[0].boxes.id is not None: | ||
# # Extract prediction results | ||
# clss = results[0].boxes.cls.cpu().tolist() | ||
# track_ids = results[0].boxes.id.int().cpu().tolist() | ||
# confs = results[0].boxes.conf.float().cpu().tolist() | ||
# frame = annotated_frame | ||
# Annotator Init | ||
|
||
# for box, cls, track_id in zip(boxes, clss, track_ids): | ||
# label_name = names[int(cls)] | ||
# # Store tracking history | ||
# track = track_history[track_id] | ||
# track.append((int((box[0] + box[2]) / 2), int((box[1] + box[3]) / 2))) | ||
# if len(track) > 30: | ||
# track.pop(0) | ||
# # Plot tracks | ||
# points = np.array(track, dtype=np.int32).reshape((-1, 1, 2)) | ||
|
||
# cv2.circle(frame, (track[-1]), 7, colors(int(cls), True), -1) | ||
|
||
# cv2.polylines(frame, [points], isClosed=False, color=colors(int(cls), True), thickness=2) | ||
# cv2.putText(frame,('count: ')+ str(len(counter_down)),(60,40),cv2.FONT_HERSHEY_SIMPLEX, 0.5, red_color, 1, cv2.LINE_AA) | ||
|
||
new_frame_time = time.time() | ||
fps = int(1/(new_frame_time-prev_frame_time)) | ||
prev_frame_time = new_frame_time | ||
|
||
infor = f"fps: {str(fps)} / count: {str(len(boxes))}" | ||
# infor = f"fps: {str(fps)}" | ||
# infor = f"count: {str(len(boxes))}" | ||
|
||
|
||
cv2.putText(frame, infor, (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, red_color , 2, cv2.LINE_AA) | ||
cv2.imshow('Frame', frame) | ||
out.write(frame) | ||
|
||
if cv2.waitKey(1) & 0xFF == ord("q"): | ||
break | ||
else: | ||
break | ||
|
||
# result.release() | ||
cap.release() | ||
out.release() | ||
cv2.destroyAllWindows() | ||
|
||
|
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 numpy as np | ||
import cv2 | ||
from shapely.geometry import LineString | ||
|
||
# Step 1: Define the line | ||
line_points = [(100, 200), (400, 500)] # Replace with your line coordinates | ||
|
||
# Step 2: Create a LineString with Shapely | ||
line = LineString(line_points) | ||
# Step 3: Offset the line | ||
offset_distance = 10 | ||
polygon = line.buffer(offset_distance, cap_style=2) # cap_style=2 for a flat end | ||
|
||
# Step 4: Convert the Polygon to a format suitable for OpenCV | ||
exterior_coords = np.array(polygon.exterior.coords, dtype=np.int32) | ||
|
||
# Step 5: Draw the Polygon with OpenCV | ||
# Create a blank image | ||
height, width = 600, 800 # Replace with your image dimensions | ||
image = np.zeros((height, width, 3), dtype=np.uint8) | ||
|
||
# Draw the polygon | ||
cv2.polylines(image, [exterior_coords], isClosed=True, color=(0, 255, 0), thickness=2) | ||
|
||
# Display the image | ||
cv2.imshow('Polygon', image) | ||
cv2.waitKey(0) | ||
cv2.destroyAllWindows() |
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,49 @@ | ||
import cv2 | ||
import numpy as np | ||
|
||
points = [] | ||
polygons = [] | ||
|
||
def click_event(event, x, y, flags, param): | ||
if event == cv2.EVENT_LBUTTONDOWN: | ||
points.append((x, y)) | ||
|
||
cv2.circle(img, (x, y), 3, (0, 0, 255), -1) | ||
cv2.imshow('image', img) | ||
|
||
if event == cv2.EVENT_RBUTTONDOWN: | ||
# Vẽ đa giác khi nhấn phím enter | ||
if len(points) > 1: | ||
cv2.polylines(img, [np.array(points)], True, (255, 0, 0), 2) | ||
cv2.imshow('image', img) | ||
polygons.append(points.copy()) | ||
points.clear() # Xóa các điểm sau khi vẽ | ||
|
||
cv2.namedWindow('image') | ||
cv2.setMouseCallback('image', click_event) | ||
|
||
img_name = '20240519_195620.png' | ||
|
||
img = cv2.imread(f'input_img/{img_name}') | ||
cv2.imshow('image', img) | ||
|
||
while True: | ||
key = cv2.waitKey(1) | ||
if key == 13: # Phím enter | ||
if len(points) > 1: | ||
cv2.polylines(img, [np.array(points)], True, (255, 0, 0), 2) | ||
cv2.imshow('image', img) | ||
polygons.append(points.copy()) | ||
points.clear() | ||
elif key == 27: # Phím ESC để thoát | ||
cv2.imwrite(f'output_img/{img_name}', img) | ||
break | ||
|
||
cv2.destroyAllWindows() | ||
|
||
polygon_name = img_name.split('.')[0] + '.txt' | ||
with open(f'polygon/{polygon_name}', 'w') as file: | ||
for polygon in polygons: | ||
for point in polygon: | ||
file.write(f"{point[0]}, {point[1]}\n") | ||
file.write("---\n") |
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,19 @@ | ||
import numpy as np | ||
|
||
def find_side_point(A, B, distance): | ||
AB_vector = np.array(B) - np.array(A) | ||
mid_point = (np.array(A) + np.array(B)) / 2 | ||
perpendicular_vector = np.array([-AB_vector[1], AB_vector[0]]) | ||
normalized_perpendicular_vector = perpendicular_vector / np.linalg.norm(perpendicular_vector) | ||
print(normalized_perpendicular_vector) | ||
C = mid_point + distance * normalized_perpendicular_vector | ||
D = mid_point - distance * normalized_perpendicular_vector | ||
return C.astype(int), D.astype(int) | ||
|
||
A = (1, 1) | ||
B = (1, 5) | ||
distance = 2 | ||
C, D = find_side_point(A, B, distance) | ||
print("C:", C) | ||
print("D:", D) | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
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,99 @@ | ||
import cv2 | ||
import numpy as np | ||
from ultralytics import YOLO | ||
import time | ||
from ultralytics.utils.checks import check_imshow | ||
from ultralytics.utils.plotting import Annotator, colors | ||
|
||
from collections import defaultdict | ||
|
||
track_history = defaultdict(lambda: []) | ||
model = YOLO("yolov8s.pt") | ||
names = model.model.names | ||
|
||
|
||
video_path = "traffictrim.mp4" | ||
cap = cv2.VideoCapture(video_path) | ||
assert cap.isOpened(), "Error reading video file" | ||
|
||
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS)) | ||
|
||
output = 'demo.avi' | ||
codec = cv2.VideoWriter_fourcc(*'XVID') | ||
out = cv2.VideoWriter(output, codec, fps, (w, h)) | ||
|
||
print(w,h) | ||
|
||
prev_frame_time = 0 | ||
new_frame_time = 0 | ||
down={} | ||
counter_down=set() | ||
while cap.isOpened(): | ||
success, frame = cap.read() | ||
if success: | ||
results = model.track(frame, persist=True, verbose=False, tracker='bytetrack.yaml', classes = [2]) | ||
boxes = results[0].boxes.xyxy.cpu() | ||
red_color = (0, 0, 255) | ||
y = 308 | ||
offset = 7 | ||
cv2.line(frame,(282,308),(1004,308),red_color,3) | ||
if results[0].boxes.id is not None: | ||
# Extract prediction results | ||
clss = results[0].boxes.cls.cpu().tolist() | ||
track_ids = results[0].boxes.id.int().cpu().tolist() | ||
confs = results[0].boxes.conf.float().cpu().tolist() | ||
annotated_frame = results[0].plot() | ||
frame = annotated_frame | ||
# Annotator Init | ||
annotator = Annotator(frame, line_width=2) | ||
|
||
for box, cls, track_id in zip(boxes, clss, track_ids): | ||
label_name = names[int(cls)] | ||
# annotator.box_label(box, color=colors(int(cls), True), label=label_name) | ||
|
||
# Store tracking history | ||
track = track_history[track_id] | ||
track.append((int((box[0] + box[2]) / 2), int((box[1] + box[3]) / 2))) | ||
if len(track) > 30: | ||
track.pop(0) | ||
# Plot tracks | ||
points = np.array(track, dtype=np.int32).reshape((-1, 1, 2)) | ||
cv2.circle(frame, (track[-1]), 7, colors(int(cls), True), -1) | ||
|
||
|
||
|
||
cv2.polylines(frame, [points], isClosed=False, color=colors(int(cls), True), thickness=2) | ||
cx = int((box[0] + box[2])/2) | ||
cy = int((box[1] + box[3])/2) | ||
|
||
if y < (cy + offset) and y > (cy - offset): | ||
|
||
|
||
|
||
if track_id not in counter_down: | ||
cv2.circle(frame,(cx,cy),4,(0,0,255),-1) | ||
cv2.putText(frame,str(len(counter_down)),(cx,cy),cv2.FONT_HERSHEY_COMPLEX,0.8,(0,255,255),2) | ||
counter_down.add(track_id) | ||
|
||
# cv2.putText(frame,('count: ')+ str(len(counter_down)),(60,40),cv2.FONT_HERSHEY_SIMPLEX, 0.5, red_color, 1, cv2.LINE_AA) | ||
|
||
new_frame_time = time.time() | ||
fps = int(1/(new_frame_time-prev_frame_time)) | ||
prev_frame_time = new_frame_time | ||
|
||
infor = f"fps: {str(fps)} / count: {str(len(counter_down))}" | ||
cv2.putText(frame, infor, (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.5, red_color , 1, cv2.LINE_AA) | ||
cv2.imshow('Frame', frame) | ||
out.write(frame) | ||
|
||
if cv2.waitKey(1) & 0xFF == ord("q"): | ||
break | ||
else: | ||
break | ||
|
||
# result.release() | ||
cap.release() | ||
out.release() | ||
cv2.destroyAllWindows() | ||
|
||
|
Oops, something went wrong.