Python+Yolov8框选位置目标识别人数统计计数

news/2024/12/2 13:39:05/

程序示例精选
Python+Yolov8框选位置目标识别人数统计计数
如需安装运行环境或远程调试,见文章底部个人QQ名片,由专业技术人员远程协助!

前言

这篇博客针对《Python+Yolov8框选位置目标识别人数统计计数》编写代码,代码整洁,规则,易读。 学习与应用推荐首选。


运行结果


文章目录

一、所需工具软件
二、使用步骤
       1. 主要代码
       2. 运行结果
三、在线协助

一、所需工具软件

       1. VS2019, Qt
       2. C++

二、使用步骤

代码如下(示例):
import cv2
from yolov8 import YOLOv8
from deep_sort_pytorch.deep_sort import DeepSort
from deep_sort_pytorch.utils.parser import get_config
from collections import deque
def init_tracker():print("init_tracker")global deepsortcfg_deep = get_config()cfg_deep.merge_from_file("deep_sort_pytorch/configs/deep_sort.yaml")deepsort= DeepSort(cfg_deep.DEEPSORT.REID_CKPT,max_dist=cfg_deep.DEEPSORT.MAX_DIST, min_confidence=cfg_deep.DEEPSORT.MIN_CONFIDENCE,nms_max_overlap=cfg_deep.DEEPSORT.NMS_MAX_OVERLAP, max_iou_distance=cfg_deep.DEEPSORT.MAX_IOU_DISTANCE,max_age=cfg_deep.DEEPSORT.MAX_AGE, n_init=cfg_deep.DEEPSORT.N_INIT, nn_budget=cfg_deep.DEEPSORT.NN_BUDGET,use_cuda=True)
##########################################################################################
def xyxy_to_xywh(*xyxy):"""" Calculates the relative bounding box from absolute pixel values. """bbox_left = min([xyxy[0].item(), xyxy[2].item()])bbox_top = min([xyxy[1].item(), xyxy[3].item()])bbox_w = abs(xyxy[0].item() - xyxy[2].item())bbox_h = abs(xyxy[1].item() - xyxy[3].item())x_c = (bbox_left + bbox_w / 2)y_c = (bbox_top + bbox_h / 2)w = bbox_wh = bbox_hreturn x_c, y_c, w, hdef xyxy_to_tlwh(bbox_xyxy):tlwh_bboxs = []for i, box in enumerate(bbox_xyxy):x1, y1, x2, y2 = [int(i) for i in box]top = x1left = y1w = int(x2 - x1)h = int(y2 - y1)tlwh_obj = [top, left, w, h]tlwh_bboxs.append(tlwh_obj)return tlwh_bboxsdef compute_color_for_labels(label):"""Simple function that adds fixed color depending on the class"""if label == 0: #personcolor = (85,45,255)elif label == 2: # Carcolor = (222,82,175)elif label == 3:  # Motobikecolor = (0, 204, 255)elif label == 5:  # Buscolor = (0, 149, 255)else:color = [int((p * (label ** 2 - label + 1)) % 255) for p in palette]return tuple(color)def draw_border(img, pt1, pt2, color, thickness, r, d):x1,y1 = pt1x2,y2 = pt2# Top leftcv2.line(img, (x1 + r, y1), (x1 + r + d, y1), color, thickness)cv2.line(img, (x1, y1 + r), (x1, y1 + r + d), color, thickness)cv2.ellipse(img, (x1 + r, y1 + r), (r, r), 180, 0, 90, color, thickness)# Top rightcv2.line(img, (x2 - r, y1), (x2 - r - d, y1), color, thickness)cv2.line(img, (x2, y1 + r), (x2, y1 + r + d), color, thickness)cv2.ellipse(img, (x2 - r, y1 + r), (r, r), 270, 0, 90, color, thickness)# Bottom leftcv2.line(img, (x1 + r, y2), (x1 + r + d, y2), color, thickness)cv2.line(img, (x1, y2 - r), (x1, y2 - r - d), color, thickness)cv2.ellipse(img, (x1 + r, y2 - r), (r, r), 90, 0, 90, color, thickness)# Bottom rightcv2.line(img, (x2 - r, y2), (x2 - r - d, y2), color, thickness)cv2.line(img, (x2, y2 - r), (x2, y2 - r - d), color, thickness)cv2.ellipse(img, (x2 - r, y2 - r), (r, r), 0, 0, 90, color, thickness)cv2.rectangle(img, (x1 + r, y1), (x2 - r, y2), color, -1, cv2.LINE_AA)cv2.rectangle(img, (x1, y1 + r), (x2, y2 - r - d), color, -1, cv2.LINE_AA)cv2.circle(img, (x1 +r, y1+r), 2, color, 12)cv2.circle(img, (x2 -r, y1+r), 2, color, 12)cv2.circle(img, (x1 +r, y2-r), 2, color, 12)cv2.circle(img, (x2 -r, y2-r), 2, color, 12)return imgdef UI_box(x, img, color=None, label=None, line_thickness=None):# Plots one bounding box on image imgtl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1  # line/font thicknesscolor = color or [random.randint(0, 255) for _ in range(3)]c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))def intersect(A,B,C,D):return ccw(A,C,D) != ccw(B,C,D) and ccw(A,B,C) != ccw(A,B,D)def ccw(A,B,C):return (C[1]-A[1]) * (B[0]-A[0]) > (B[1]-A[1]) * (C[0]-A[0])def get_direction(point1, point2):direction_str = ""def draw_boxes(img, bbox, names,object_id, identities=None, offset=(0, 0)):cv2.line(img, line[0], line[1], (0,0,255), 3)height, width, _ = img.shape# remove tracked point from buffer if object is lostfor key in list(data_deque):if key not in identities:data_deque.pop(key)for i, box in enumerate(bbox):x1, y1, x2, y2 = [int(i) for i in box]x1 += offset[0]x2 += offset[0]y1 += offset[1]y2 += offset[1]# code to find center of bottom edgecenter = (int((x2+x1)/ 2), int((y2+y2)/2))# get ID of objectid = int(identities[i]) if identities is not None else 0# create new buffer for new objectif id not in data_deque:  data_deque[id] = deque(maxlen= 64)color = compute_color_for_labels(object_id[i])obj_name = names[object_id[i]]label = '{}{:d}'.format("", id) + ":"+ '%s' % (obj_name)# add center to bufferdata_deque[id].appendleft(center)if len(data_deque[id]) >= 2:direction = get_direction(data_deque[id][0], data_deque[id][1])if intersect(data_deque[id][0], data_deque[id][1], line[0], line[1]):cv2.line(img, line[0], line[1], (255, 255, 255), 3)if "South" in direction:if obj_name not in object_counter:object_counter[obj_name] = 1else:object_counter[obj_name] += 1if "North" in direction:if obj_name not in object_counter1:object_counter1[obj_name] = 1else:object_counter1[obj_name] += 1UI_box(box, img, label=label, color=color, 
运行结果


在这里插入图片描述

三、在线协助:

如需安装运行环境或远程调试,见文章底部个人 QQ 名片,由专业技术人员远程协助!

1)远程安装运行环境,代码调试
2)Visual Studio, Qt, C++, Python编程语言入门指导
3)界面美化
4)软件制作
5)云服务器申请
6)网站制作

当前文章连接:https://blog.csdn.net/alicema1111/article/details/132666851
个人博客主页:https://blog.csdn.net/alicema1111?type=blog
博主所有文章点这里:https://blog.csdn.net/alicema1111?type=blog

博主推荐:
Python人脸识别考勤打卡系统:
https://blog.csdn.net/alicema1111/article/details/133434445
Python果树水果识别:https://blog.csdn.net/alicema1111/article/details/130862842
Python+Yolov8+Deepsort入口人流量统计:https://blog.csdn.net/alicema1111/article/details/130454430
Python+Qt人脸识别门禁管理系统:https://blog.csdn.net/alicema1111/article/details/130353433
Python+Qt指纹录入识别考勤系统:https://blog.csdn.net/alicema1111/article/details/129338432
Python Yolov5火焰烟雾识别源码分享:https://blog.csdn.net/alicema1111/article/details/128420453
Python+Yolov8路面桥梁墙体裂缝识别:https://blog.csdn.net/alicema1111/article/details/133434445
Python+Yolov5道路障碍物识别:https://blog.csdn.net/alicema1111/article/details/129589741
Python+Yolov5跌倒检测 摔倒检测 人物目标行为 人体特征识别:https://blog.csdn.net/alicema1111/article/details/129272048


http://www.ppmy.cn/news/1408953.html

相关文章

IDEA2023创建SpringMVC项目

✅作者简介:大家好,我是Leo,热爱Java后端开发者,一个想要与大家共同进步的男人😉😉 🍎个人主页:Leo的博客 💞当前专栏: 开发环境篇 ✨特色专栏: M…

2012年认证杯SPSSPRO杯数学建模C题(第二阶段)碎片化趋势下的奥运会商业模式全过程文档及程序

2012年认证杯SPSSPRO杯数学建模 C题 碎片化趋势下的奥运会商业模式 原题再现: 从 1984 年的美国洛杉矶奥运会开始,奥运会就不在成为一个“非卖品”,它在向观众诠释更高更快更强的体育精神的同时,也在攫取着巨大的商业价值&#…

OpenHarmony开发-系统烧录

本文详细介绍了烧录OpenHarmony系统到开发板的操作流程。从基础的硬件准备和软件环境设置入手,详细说明了如何配置开发环境、构建系统镜像等过程,详细描述了烧录过程中的关键步骤,以及如何使用专用工具将OpenHarmony系统镜像传输到开发板。同…

线性表之——顺序表

哈喽小伙伴们大家好,这篇博客呢,鱼头会和大家分享一下我最近学习的数据结构中的顺序表,希望能对在读的各位提供帮助,还望多多支持! 目录 1.顺序表 1.1线性表 1.2顺序表的分类 1.2.1静态顺序表 1.2.2动态顺序表 …

基础算法精讲 07|环形链表II,快慢指针

876. 链表的中间结点 - 力扣(LeetCode) Time: O(n) Space:O(1) /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}* ListNode(int val) { this.val val; }* ListNod…

python爬虫获取豆瓣前top250的标题(简单)

今天是简略的一篇,简单小实验 import requests from bs4 import BeautifulSoup# 模拟浏览器的构成(请求头) headers {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Ch…

github本地仓库push到远程仓库

1.从远程仓库clone到本地 2.生成SSH秘钥&#xff0c;为push做准备 在Ubuntu命令行输入一下内容 [rootlocalhost ~]# ssh-keygen -t rsa < 建立密钥对&#xff0c;-t代表类型&#xff0c;有RSA和DSA两种 Generating public/private rsa key pair. Enter file in whi…

消息中间件之消息通信模型MQ

一&#xff0c;为什么需要MQ&#xff1f; 应用中&#xff0c;经常需要对庞大的海量数据进行监控&#xff0c;随着网络技术和软件开发技术的不断提高&#xff0c;在实战开发中MQ的使用与日俱增&#xff0c;特别是RabbitMQ在分布式系统中存储转发消息&#xff0c;可以保证数据不…