Upload 16 files
Browse filesFiles changed (17)
- .gitattributes+2
- BJP1.mp4Added+3
- BJ_outputAdded
- Readme.mdAdded+17
- best.ptAdded+3
- best1.ptAdded+3
- coco.namesAdded+84
- coco2.namesAdded+7
- coffee.jpgAdded
- ds.jpgAdded
- dynamic.pyAdded+84
- false_smoke_detect.pyAdded+81
- normal_picture_detect.pyAdded+80
- plane_check.mp4Added+3
- shi_pin_bike_detect.pyAdded+104
- shi_pin_plane_count.pyAdded+104
- yolov8x.ptAdded+3
32 unmodified lines33343532 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text32 unmodified lines333435363732 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -textBJP1.mp4 filter=lfs diff=lfs merge=lfs -textplane_check.mp4 filter=lfs diff=lfs merge=lfs -text
123version https://git-lfs.github.com/spec/v1oid sha256:d680bccc8e5309c0730f168db9a3b65253fb140cbb04c332227370f28188acddsize 970688891234567891011121314151617For this project, there are 5 different
Running and install instructions
1. for the normal_picture_detect, please install all the nessary packages in the requirements.txt file.(some of the libaries may
not be able use in MacOS)
2. for the shi_pin_bike_detect, this is detecting for most objects on the road in Beijing(video taken by myself so no copyright issues)
if the code didn't work, try change the path of the video in the code to the absolute path of the video on your computer(because it works on mine).
3. The shi_pin_plane_count is mainly build for the satellite(maybe military use). The program can detect planes from top view and draw out
all(70%-87% accuracy) of the plane show in picture or every frame of the video. It should be using CUDA to run it but if not, it will run on CPU.
4. The dynamic is a use of the camera detection for 80 different types of objects.
5. The false_smoke_detect is not successful yet due to small number of epochs and image_size. It will mainly detect things correct but with wrong name
attached to it. It's build for abnormal driving behaviour detect.
Problem faced:
1. Apple Silicon mps error from YOLOv7(benched model for our project)
2. MPS efficiency not working great
3. CUDA not working on my computer(Torch and CUDA toolkit problem)
4. Training error123version https://git-lfs.github.com/spec/v1oid sha256:6302b14c17b98dbfb9e9620889520378c9a58bb5044aeba7e65f570d7a03f560size 155704400123version https://git-lfs.github.com/spec/v1oid sha256:07d3bb3098bd936fe3bdc930e647784475e771ae00da50d79af8259a7d91e25dsize 87640435123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384personbicyclecarmotorbikeairplanebustraintruckboattraffic lightfire hydrantstop signparking meterbenchbirdcatdoghorsesheepcowelephantbearzebragiraffebackpackumbrellahandbagtiesuitcasefrisbeeskissnowboardsports ballkitebaseball batbaseball gloveskateboardsurfboardtennis racketbottlewine glasscupforkknifespoonbowlbananaapplesandwichorangebroccolicarrothot dogpizzadonutcakechairsofapottedplantbeddiningtabletoilettvmonitorlaptopmouseremotekeyboardphonemicrowaveoventoastersinkrefrigeratorbookclockvasescissorsteddy bearhair driertoothbrushtraffic-yellowtraffic-redtraffic-greenConst-sign1234567airplane
Cigarette
Drinking
Eating
Phone
Seatbelt
no seatbelt123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384import cv2
import numpy as np
from collections import Counter
from ultralytics import YOLO
# 加载 YOLOv8 模型
model = YOLO('yolov8x.pt')
# 加载类别名称
with open("coco.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
# 打开摄像头
cap = cv2.VideoCapture(0) # 0 表示默认摄像头
if not cap.isOpened():
print("Error: Cannot access the camera.")
exit()
print("Press 'q' to exit.")
while True:
# 从摄像头读取帧
ret, frame = cap.read()
if not ret:
print("Error: Failed to capture image from camera.")
break
height, width, channels = frame.shape
# 执行推理
results = model(frame)[0]
# 初始化检测结果
class_ids = []
confidences = []
boxes = []
# 解析检测结果
for result in results.boxes.data.tolist():
x1, y1, x2, y2, confidence, class_id = result
# 过滤置信度低的检测
if confidence > 0.3:
# 计算边界框参数
x = int(x1)
y = int(y1)
w = int(x2 - x1)
h = int(y2 - y1)
# 保存检测结果
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(int(class_id))
# 使用非最大抑制(NMS)去除重复框
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
# 初始化一个计数器来记录类别出现次数
detected_objects = []
# 输出检测结果
if len(indexes) > 0:
for i in indexes.flatten():
x, y, w, h = boxes[i]
label = str(classes[class_ids[i]])
confidence = confidences[i]
detected_objects.append(label)
# 绘制边界框和类别标签
color = (0, 255, 0)
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
cv2.putText(frame, f"{label} {confidence:.2f}",
(x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
# 显示检测结果
cv2.imshow("Real-Time Object Detection", frame)
# 按 'q' 键退出
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放摄像头资源并关闭所有窗口
cap.release()
cv2.destroyAllWindows()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081import cv2
import numpy as np
from collections import Counter
from ultralytics import YOLO
# 加载 YOLOv8 模型
# model = YOLO('yolov8l.pt')
model = YOLO('best.pt')
# 加载类别名称
with open("coco2.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
# 加载输入图像
image_path = "coffee.jpg" # 替换为你的图片路径
image = cv2.imread(image_path)
height, width, channels = image.shape
# 执行推理
results = model(image)[0]
# 初始化检测结果
class_ids = []
confidences = []
boxes = []
# 解析检测结果
for result in results.boxes.data.tolist():
x1, y1, x2, y2, confidence, class_id = result
# 过滤置信度低的检测
if confidence > 0.3:
# 计算边界框参数
x = int(x1)
y = int(y1)
w = int(x2 - x1)
h = int(y2 - y1)
# 保存检测结果
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(int(class_id))
# 使用非最大抑制(NMS)去除重复框
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
# 初始化一个计数器来记录类别出现次数
detected_objects = []
# 输出检测结果
if len(indexes) > 0:
print("Detected objects in the image:")
for i in indexes.flatten():
x, y, w, h = boxes[i]
label = str(classes[class_ids[i]])
confidence = confidences[i]
print(f"Class: {label}, Confidence: {confidence:.2f}, Box: ({x}, {y}, {w}, {h})")
detected_objects.append(label)
# 绘制边界框和类别标签
color = (0, 255, 0)
cv2.rectangle(image, (x, y), (x + w, y + h), color, 2)
cv2.putText(image, f"{label} {confidence:.2f}",
(x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
# 显示图像
cv2.imshow("Detected Objects", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
print("No objects detected in the image.")
# 输出识别到的类别和数量
if detected_objects:
object_counts = Counter(detected_objects)
print("\nSummary of detected objects:")
for obj, count in object_counts.items():
print(f"{obj}: {count}")
else:
print("\nNo objects detected in the image.")1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980import cv2
import numpy as np
from collections import Counter
from ultralytics import YOLO
# 加载 YOLOv8 模型
model = YOLO('yolov8x.pt')
# 加载类别名称
with open("coco2.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
# 加载输入图像
image_path = "plane_count.jpg" # 替换为你的图片路径
image = cv2.imread(image_path)
height, width, channels = image.shape
# 执行推理
results = model(image)[0]
# 初始化检测结果
class_ids = []
confidences = []
boxes = []
# 解析检测结果
for result in results.boxes.data.tolist():
x1, y1, x2, y2, confidence, class_id = result
# 过滤置信度低的检测
if confidence > 0.3:
# 计算边界框参数
x = int(x1)
y = int(y1)
w = int(x2 - x1)
h = int(y2 - y1)
# 保存检测结果
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(int(class_id))
# 使用非最大抑制(NMS)去除重复框
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
# 初始化一个计数器来记录类别出现次数
detected_objects = []
# 输出检测结果
if len(indexes) > 0:
print("Detected objects in the image:")
for i in indexes.flatten():
x, y, w, h = boxes[i]
label = str(classes[class_ids[i]])
confidence = confidences[i]
print(f"Class: {label}, Confidence: {confidence:.2f}, Box: ({x}, {y}, {w}, {h})")
detected_objects.append(label)
# 绘制边界框和类别标签
color = (0, 255, 0)
cv2.rectangle(image, (x, y), (x + w, y + h), color, 2)
cv2.putText(image, f"{label} {confidence:.2f}",
(x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
# 显示图像
cv2.imshow("Detected Objects", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
print("No objects detected in the image.")
# 输出识别到的类别和数量
if detected_objects:
object_counts = Counter(detected_objects)
print("\nSummary of detected objects:")
for obj, count in object_counts.items():
print(f"{obj}: {count}")
else:
print("\nNo objects detected in the image.")123version https://git-lfs.github.com/spec/v1oid sha256:dbaf0253cf7ebbae27eea3cb596b3160f6fb663c95e7845e1fec38d5faa3bee5size 3145642123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104import cv2
import numpy as np
from collections import Counter
from ultralytics import YOLO
# # 加载 YOLOv8 模型
# model = YOLO(r'C:\Users\Micha\Desktop\p2\runs\detect\train25\weights\best.pt')
model = YOLO('yolov8x.pt')
# 加载类别名称
with open("coco.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
# 视频文件路径
video_path = r"BJP1.mp4" # 输入视频文件路径
output_path = "BJ_output" # 输出视频保存路径
# 打开视频文件
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print(f"Error: Cannot open video {video_path}")
exit()
# 获取视频属性
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
# 定义视频输出格式
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
print(f"Processing video: {video_path}")
print("Press 'q' to stop processing.")
while True:
# 读取视频帧
ret, frame = cap.read()
if not ret:
print("End of video or cannot read the frame.")
break
# 执行推理
results = model(frame)[0]
# 初始化检测结果
class_ids = []
confidences = []
boxes = []
# 解析检测结果
for result in results.boxes.data.tolist():
x1, y1, x2, y2, confidence, class_id = result
# 过滤置信度低的检测
if confidence > 0.7:
# 计算边界框参数
x = int(x1)
y = int(y1)
w = int(x2 - x1)
h = int(y2 - y1)
# 保存检测结果
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(int(class_id))
# 使用非最大抑制(NMS)去除重复框
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
# 初始化一个计数器来记录类别出现次数
detected_objects = []
# 绘制检测结果
if len(indexes) > 0:
for i in indexes.flatten():
x, y, w, h = boxes[i]
label = str(classes[class_ids[i]])
confidence = confidences[i]
detected_objects.append(label)
# 绘制边界框和类别标签
color = (0, 255, 0)
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
cv2.putText(frame, f"{label} {confidence:.2f}",
(x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
# 保存处理后的帧到输出视频
out.write(frame)
# 显示处理后的帧
cv2.imshow("Video Detection", frame)
# 按下 'q' 键退出
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放资源
cap.release()
out.release()
cv2.destroyAllWindows()
print(f"Processed video saved to: {output_path}")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104import cv2
import numpy as np
from collections import Counter
from ultralytics import YOLO
# 加载 YOLOv8 模型
model = YOLO('best1.pt')
print(f"Running on device: {model.device}") # 输出设备信息
# 加载类别名称
with open("coco2.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
# 视频文件路径
video_path = r"C:\Users\Micha\Desktop\p2\plane_check.mp4" # 输入视频文件路径
output_path = "C:/Users/Micha/Desktop/p2/output_video.mp4" # 输出视频保存路径
# 打开视频文件
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print(f"Error: Cannot open video {video_path}")
exit()
# 获取视频属性
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
# 定义视频输出格式
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
print(f"Processing video: {video_path}")
print("Press 'q' to stop processing.")
while True:
# 读取视频帧
ret, frame = cap.read()
if not ret:
print("End of video or cannot read the frame.")
break
# 执行推理
results = model(frame)[0]
# 初始化检测结果
class_ids = []
confidences = []
boxes = []
# 解析检测结果
for result in results.boxes.data.tolist():
x1, y1, x2, y2, confidence, class_id = result
# 过滤置信度低的检测
if confidence > 0.7:
# 计算边界框参数
x = int(x1)
y = int(y1)
w = int(x2 - x1)
h = int(y2 - y1)
# 保存检测结果
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(int(class_id))
# 使用非最大抑制(NMS)去除重复框
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
# 初始化一个计数器来记录类别出现次数
detected_objects = []
# 绘制检测结果
if len(indexes) > 0:
for i in indexes.flatten():
x, y, w, h = boxes[i]
label = str(classes[class_ids[i]])
confidence = confidences[i]
detected_objects.append(label)
# 绘制边界框和类别标签
color = (0, 255, 0)
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
cv2.putText(frame, f"{label} {confidence:.2f}",
(x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
# 保存处理后的帧到输出视频
out.write(frame)
# 显示处理后的帧
cv2.imshow("Video Detection", frame)
# 按下 'q' 键退出
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放资源
cap.release()
out.release()
cv2.destroyAllWindows()
print(f"Processed video saved to: {output_path}")123version https://git-lfs.github.com/spec/v1oid sha256:3df4ada6b4dad6d657868f2fdf7faecfb34dcfccf3a25c4b82079064718524c8size 136890692.gitattributes+2
BJP1.mp4Added+3
BJ_outputAdded
Readme.mdAdded+17
best.ptAdded+3
best1.ptAdded+3
coco.namesAdded+84
coco2.namesAdded+7
coffee.jpgAdded

ds.jpgAdded

dynamic.pyAdded+84
false_smoke_detect.pyAdded+81
normal_picture_detect.pyAdded+80
plane_check.mp4Added+3
shi_pin_bike_detect.pyAdded+104
shi_pin_plane_count.pyAdded+104
yolov8x.ptAdded+3