当前位置:   article > 正文

openCV实战练习(5)—— 物体检测_opencv物体检测

opencv物体检测

项目Introduce:

项目名称:

在冰红茶的博客看到的这个实战,物体检测应该是对于视觉在实用性上的一门基础课,上手实践的时候做了一些小小的改动,顺利让代码跑起来了

Opencv项目实战:05 物体检测_opencv物体检测_夏天是冰红茶的博客-CSDN博客

视频转图像-cv2.VideoCapture()用法_宁然也的博客-CSDN博客

运行报错error: (-215:Assertion failed) !ssize.empty() in function ‘cv::resize‘_努力搬砖的小菜鸟的博客-CSDN博客

项目流程预览:

1.读取GIF or 调用摄像头

2.读取文件‘coco.name’(一个放了各种物体名词的文件)

3.调用库,实行匹 配

4.展示

项目与知识衔接:

Object Detection OpenCV Python  Easy and Fast (2020).jpg

ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt

NMS

...

具体操作步骤以及代码:

1.读取GIF or 调用摄像头 cv2.VideoCapture()

  1. thres = 0.45# threshold to detect object
  2. #use nms to avoid cover and flash
  3. nms_threshold = 0.2 #较大抑制效果
  4. #if =1 没有效果
  5. cap = cv2.VideoCapture(0)
  6. cap.set(3,1280)
  7. cap.set(4,720)
  8. cap.set(10,150)

2.读取文件

  1. classNames = []
  2. classFile = 'coco.names'
  3. with open(classFile,'rt') as f:
  4. classNames= f.read().rstrip('\n').split('\n')

3.调用

Object Detection OpenCV Python  Easy and Fast (2020).jpg

ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt

  1. configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
  2. weightsPath = 'frozen_inference_graph.pb'
  3. net = cv2.dnn_DetectionModel(weightsPath,configPath)
  4. net.setInputSize(320,320)
  5. net.setInputScale(1.0/ 127.5)
  6. net.setInputMean((127.5,127.5,127.5))
  7. net.setInputSwapRB(True)

4.展示结果

  1. while True:
  2. success,img = cap.read()
  3. classIds,confs,bbox = net.detect(img,confThreshold=thres)
  4. bbox = list(bbox)
  5. #bbox 本为numpy数组,后将其改为list
  6. confs = list(np.array(confs).reshape(1,-1)[0])
  7. #将内容转化为一个列表,使用np.array()是因为元组不可reshape
  8. confs = list(map(float,confs))
  9. #confs 本为float32,使用map()将float映射于confs上
  10. # print(type(confs[0]))
  11. # print(confs)
  12. indices = cv2.dnn.NMSBoxes(bbox, confs, thres, nms_threshold)
  13. print(indices)
  14. print(type(indices))
  15. for i in indices:
  16. print(type(i))
  17. # i=i[0]
  18. #打印(indices)的内容是[[0]]
  19. box = bbox[i]
  20. x,y,w,h = box[0],box[1],box[2],box[3]
  21. cv2.rectangle(img,(x,y),(x+w,y+h),color=(0,255,0),thickness=2)
  22. cv2.putText(img,classNames[classIds[i]-1].upper(),(box[0]+10,box[1]+30),cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
  23. #此处classIds拥有特殊索引i,且已有[],所以是[i]
  24. cv2.imshow("Output",img)
  25. if cv2.waitKey(1) & 0xFF ==27:
  26. break

项目所需要的工具以及预设参数:

关于文件调用的Github可以在冰红茶的blog里找,很详细

实现结果展示:

 

代码整体展示:

  1. import cv2
  2. import numpy as np
  3. thres = 0.45# threshold to detect object
  4. #use nms to avoid cover and flash
  5. nms_threshold = 0.2 #较大抑制效果
  6. #if =1 没有效果
  7. cap = cv2.VideoCapture(0)
  8. cap.set(3,1280)
  9. cap.set(4,720)
  10. cap.set(10,150)
  11. classNames = []
  12. classFile = 'coco.names'
  13. with open(classFile,'rt') as f:
  14. classNames= f.read().rstrip('\n').split('\n')
  15. # print(classNames)
  16. configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
  17. weightsPath = 'frozen_inference_graph.pb'
  18. net = cv2.dnn_DetectionModel(weightsPath,configPath)
  19. net.setInputSize(320,320)
  20. net.setInputScale(1.0/ 127.5)
  21. net.setInputMean((127.5,127.5,127.5))
  22. net.setInputSwapRB(True)
  23. while True:
  24. success,img = cap.read()
  25. classIds,confs,bbox = net.detect(img,confThreshold=thres)
  26. bbox = list(bbox)
  27. #bbox 本为numpy数组,后将其改为list
  28. confs = list(np.array(confs).reshape(1,-1)[0])
  29. #将内容转化为一个列表,使用np.array()是因为元组不可reshape
  30. confs = list(map(float,confs))
  31. #confs 本为float32,使用map()将float映射于confs上
  32. # print(type(confs[0]))
  33. # print(confs)
  34. indices = cv2.dnn.NMSBoxes(bbox, confs, thres, nms_threshold)
  35. print(indices)
  36. print(type(indices))
  37. for i in indices:
  38. print(type(i))
  39. # i=i[0]
  40. #打印(indices)的内容是[[0]]
  41. box = bbox[i]
  42. x,y,w,h = box[0],box[1],box[2],box[3]
  43. cv2.rectangle(img,(x,y),(x+w,y+h),color=(0,255,0),thickness=2)
  44. cv2.putText(img,classNames[classIds[i]-1].upper(),(box[0]+10,box[1]+30),cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
  45. #此处classIds拥有特殊索引i,且已有[],所以是[i]
  46. cv2.imshow("Output",img)
  47. if cv2.waitKey(1) & 0xFF ==27:
  48. break
  49. #检测物体

代码运用是上手很快的,总体运行的步骤很简单(虽然有些算法看不懂,但发觉一点就是,在出现报错的时候,我对报错类型的反应变快了,debug的效率提高了很多,感觉有在进步的。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/765127
推荐阅读
相关标签
  

闽ICP备14008679号