You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

camera.py 11 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # @Author : linjie
  4. # detection: 基于yolov5+deepsort(目标跟踪)+Flask Video Streaming实现浏览器打开摄像头 进行目标跟踪
  5. import os
  6. import cv2
  7. from base_camera import BaseCamera
  8. from models.experimental import attempt_load
  9. import torch.backends.cudnn as cudnn
  10. import torch
  11. import torch.nn as nn
  12. import torchvision
  13. import numpy as np
  14. import argparse
  15. from utils.datasets import *
  16. from utils.utils import *
  17. from utils.torch_utils import select_device, load_classifier, time_synchronized
  18. from flask import Flask,url_for
  19. from log import Logger
  20. from deep_sort_pytorch.deep_sort import DeepSort
  21. from deep_sort_pytorch.utils.parser import get_config
  22. class Camera(BaseCamera):
  23. video_source = 0
  24. palette = (2 ** 11 - 1, 2 ** 15 - 1, 2 ** 20 - 1)
  25. #video_source = 0
  26. def __init__(self):
  27. if os.environ.get('OPENCV_CAMERA_SOURCE'):
  28. print('走了吗')
  29. Camera.set_video_source(int(os.environ['OPENCV_CAMERA_SOURCE']))
  30. super(Camera, self).__init__()
  31. print('走了')
  32. @staticmethod
  33. def set_video_source(source):
  34. Camera.video_source = source
  35. @staticmethod
  36. def bbox_rel(*xyxy):
  37. """" Calculates the relative bounding box from absolute pixel values. """
  38. bbox_left = min([xyxy[0].item(), xyxy[2].item()])
  39. bbox_top = min([xyxy[1].item(), xyxy[3].item()])
  40. bbox_w = abs(xyxy[0].item() - xyxy[2].item())
  41. bbox_h = abs(xyxy[1].item() - xyxy[3].item())
  42. x_c = (bbox_left + bbox_w / 2)
  43. y_c = (bbox_top + bbox_h / 2)
  44. w = bbox_w
  45. h = bbox_h
  46. return x_c, y_c, w, h
  47. @staticmethod
  48. def compute_color_for_labels(label):
  49. """
  50. Simple function that adds fixed color depending on the class
  51. """
  52. color = [int((p * (label ** 2 - label + 1)) % 255) for p in Camera.palette]
  53. return tuple(color)
  54. # @staticmethod
  55. # def people_appeal1():
  56. # a = 'people'
  57. # yield a
  58. @staticmethod
  59. def draw_boxes(img, bbox, cls_names, scores, identities=None, offset=(0,0)):
  60. for i, box in enumerate(bbox):
  61. x1, y1, x2, y2 = [int(i) for i in box]
  62. x1 += offset[0]
  63. x2 += offset[0]
  64. y1 += offset[1]
  65. y2 += offset[1]
  66. # box text and bar
  67. id = int(identities[i]) if identities is not None else 0
  68. color = Camera.compute_color_for_labels(id)
  69. label = '%d %s %d' % (id, cls_names[i], scores[i])
  70. label += '%'
  71. print("{0}号人物出现!========================================".format(id))
  72. t_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_PLAIN, 2, 2)[0]
  73. cv2.rectangle(img, (x1, y1), (x2, y2), color, 3)
  74. cv2.rectangle(
  75. img, (x1, y1), (x1 + t_size[0] + 3, y1 + t_size[1] + 4), color, -1)
  76. cv2.putText(img, label, (x1, y1 +
  77. t_size[1] + 4), cv2.FONT_HERSHEY_PLAIN, 2, [255, 255, 255], 2)
  78. return img
  79. @staticmethod
  80. def frames():
  81. logger = Logger()
  82. print('初始化过了,。。。。。')
  83. camera = cv2.VideoCapture(Camera.video_source)
  84. if not camera.isOpened():
  85. raise RuntimeError('Could not start camera.')
  86. out, weights, imgsz = \
  87. '.inference/output', 'weights/yolov5s.pt', 640
  88. source = "0"
  89. # print(source)
  90. # print(type(source))
  91. webcam = source.isnumeric()
  92. # print('看看webcam:{0}'.format(webcam))
  93. '''
  94. 初始化deepsort
  95. '''
  96. # initialize deepsort
  97. cfg = get_config()
  98. cfg.merge_from_file('deep_sort_pytorch/configs/deep_sort.yaml')
  99. deepsort = DeepSort(cfg.DEEPSORT.REID_CKPT,
  100. max_dist=cfg.DEEPSORT.MAX_DIST, min_confidence=cfg.DEEPSORT.MIN_CONFIDENCE,
  101. nms_max_overlap=cfg.DEEPSORT.NMS_MAX_OVERLAP,
  102. max_iou_distance=cfg.DEEPSORT.MAX_IOU_DISTANCE,
  103. max_age=cfg.DEEPSORT.MAX_AGE, n_init=cfg.DEEPSORT.N_INIT, nn_budget=cfg.DEEPSORT.NN_BUDGET,
  104. use_cuda=True)
  105. device = torch_utils.select_device()
  106. # print(weights)
  107. # print(os.getcwd())
  108. if os.path.exists(out):
  109. shutil.rmtree(out) # delete output folder
  110. os.makedirs(out) # make new output folder
  111. #shutil.rmtree(out)
  112. # Load model
  113. # google_utils.attempt_download(weights)
  114. # model = torch.load(weights, map_location=device)['model']
  115. model = attempt_load(weights, map_location=device) # load FP32 model
  116. model.to(device).eval()
  117. # Second-stage classifier
  118. classify = False
  119. if classify:
  120. modelc = load_classifier(name='resnet101', n=2) # initialize
  121. modelc.load_state_dict(torch.load('weights/resnet101.pt', map_location=device)['model']) # load weights
  122. modelc.to(device).eval()
  123. # Half precision
  124. half = False and device.type != 'cpu'
  125. # print('half = ' + str(half))
  126. if half:
  127. model.half()
  128. # Set Dataloader
  129. vid_path, vid_writer = None, None
  130. # #if webcam:
  131. # view_img = True
  132. # cudnn.benchmark = True # set True to speed up constant image size inference
  133. # dataset = LoadStreams(source, img_size=imgsz)
  134. # else:
  135. # save_img = True
  136. # # # 如果检测视频的时候想显示出来,可以在这里加一行view_img = True
  137. # view_img = True
  138. # dataset = LoadImages(source, img_size=imgsz)
  139. # vid_path, vid_writer = None, None
  140. #dataset = LoadImages(source, img_size=imgsz)
  141. dataset = LoadStreams(source, img_size=imgsz)
  142. # print('看看dataset:{0}'.format(dataset))
  143. names = model.names if hasattr(model, 'names') else model.modules.names
  144. # print('----')
  145. # print(names)
  146. colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))]
  147. # Run inference
  148. t0 = time.time()
  149. img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img
  150. _ = model(img.half() if half else img) if device.type != 'cpu' else None # run once
  151. for frame_idx, (path, img, im0s, vid_cap) in enumerate(dataset):
  152. # print('path:{0}'.format(path))
  153. # print('im0s:{0}'.format(im0s))
  154. # print('im0s类型:{0}'.format(type(im0s)))
  155. img = torch.from_numpy(img).to(device)
  156. img = img.half() if half else img.float() # uint8 to fp16/32
  157. img /= 255.0 # 0 - 255 to 0.0 - 1.0
  158. if img.ndimension() == 3:
  159. img = img.unsqueeze(0)
  160. # Inference
  161. t1 = torch_utils.time_synchronized()
  162. pred = model(img, augment=False)[0]
  163. # Apply NMS
  164. pred = non_max_suppression(pred, 0.4, 0.5,
  165. fast=True, classes=None, agnostic=False)
  166. t2 = torch_utils.time_synchronized()
  167. # Apply Classifier
  168. if classify:
  169. pred = apply_classifier(pred, modelc, img, im0s)
  170. for i, det in enumerate(pred): # detections per image
  171. #p, s, im0 = path, '', im0s
  172. p, s, im0 = path[i], '%g: ' % i, im0s[i].copy()
  173. #save_path = str(Path(out) / Path(p).name)
  174. s += '%gx%g ' % img.shape[2:] # print string
  175. #gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] #  normalization gain whwh
  176. if det is not None and len(det):
  177. # Rescale boxes from img_size to im0 size
  178. det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round()
  179. #for c in det[:, -1].unique(): #probably error with torch 1.5
  180. for c in det[:, -1].detach().unique():
  181. n = (det[:, -1] == c).sum() # detections per class
  182. s += '%g %s, ' % (n, names[int(c)]) # add to string
  183. # --- linjie
  184. bbox_xywh = []
  185. confs = []
  186. clses = []
  187. # for *xyxy, conf, cls in det:
  188. # label = '%s %.2f' % (names[int(cls)], conf)
  189. # plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)
  190. # Write results
  191. for *xyxy, conf, cls in reversed(det):
  192. # -- linjie deepsort
  193. x_c, y_c, bbox_w, bbox_h = Camera.bbox_rel(*xyxy)
  194. obj = [x_c, y_c, bbox_w, bbox_h]
  195. bbox_xywh.append(obj)
  196. confs.append([conf.item()])
  197. clses.append([cls.item()])
  198. label = '%s %.2f' % (names[int(cls)], conf)
  199. print('看看这次打的标签:{0}'.format(label))
  200. #plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)
  201. # 判断标签是否为人 --linjie
  202. if label is not None:
  203. if (label.split())[0] == 'person':
  204. logger.info('当前进程:{0}.遇到了人'.format(os.getpid()))
  205. #print('标签是人')
  206. # distancing(people_coords, im0, dist_thres_lim=(200, 250))
  207. # people_coords.append(xyxy)
  208. #plot_one_box(xyxy, im0, line_thickness=3)
  209. plot_dots_on_people(xyxy, im0)
  210. # ---linjie deepsort
  211. xywhs = torch.Tensor(bbox_xywh)
  212. confss = torch.Tensor(confs)
  213. clses = torch.Tensor(clses)
  214. outputs = deepsort.update(xywhs, confss, clses,im0)
  215. # draw boxes for visualization
  216. if len(outputs) > 0:
  217. bbox_tlwh = []
  218. bbox_xyxy = outputs[:, :4]
  219. identities = outputs[:, 4]
  220. clses = outputs[:, 5]
  221. scores = outputs[:, 6]
  222. stays = outputs[:, 7]
  223. Camera.draw_boxes(im0, bbox_xyxy, [names[i] for i in clses], scores, identities)
  224. print('%sDone. (%.3fs)' % (s, t2 - t1))
  225. # Stream results
  226. # if view_img:
  227. # cv2.imshow(p, im0)
  228. # if cv2.waitKey(1) == ord('q'): # q to quit
  229. # raise StopIteration
  230. yield cv2.imencode('.jpg', im0)[1].tobytes()

随着人工智能和大数据的发展,任一方面对自动化工具有着一定的需求,在当下疫情防控期间,使用mindspore来实现yolo模型来进行目标检测及语义分割,对视频或图片都可以进行口罩佩戴检测和行人社交距离检测,来对公共场所的疫情防控来实行自动化管理。