当前位置:   article > 正文

无人机目标检测

无人机目标检测

导师给了个练手项目,记录下第一个项目过程:

项目要求:检测无人机降落时是否偏移,识别降落点。

想法是用opencv,使用传统的轮廓检测

首先把视频流转换成图片:只提取最后两分钟降落的片段

  1. import cv2
  2. import os
  3. dir=r"..\wrong\video"
  4. file_name="2022-06-07.mp4"
  5. if os.path.exists(dir+"/img")==False:
  6. os.makedirs(dir+"/img")
  7. file=os.path.join(dir,file_name)
  8. cap=cv2.VideoCapture(file)
  9. fps=cap.get(cv2.CAP_PROP_FPS)
  10. frame_num=cap.get(cv2.CAP_PROP_FRAME_COUNT)
  11. (width,height)=(cap.get(cv2.CAP_PROP_FRAME_WIDTH),cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
  12. print(width,height,fps,frame_num)
  13. num=0
  14. while True:
  15. ret,frame=cap.read()
  16. if ret==False:
  17. break
  18. if num>frame_num-fps*120:
  19. export_path=os.path.join(dir,"img",file_name.split('.')[0]+str(num)+'.jpg')
  20. #print(export_path)
  21. a=cv2.imwrite(export_path,frame)
  22. #print(a)
  23. num+=1
  24. print(num)

 对图像进行分割,根据面积和颜色特征提取降落点信息:

  1. import cv2
  2. import os
  3. import numpy as np
  4. from tqdm import tqdm
  5. dir=r"..\video\CAPTURE_2022-06-07-14-35-13_img\descend"
  6. files=os.listdir(dir)
  7. qrcoder=cv2.QRCodeDetector()
  8. writer = cv2.VideoWriter(dir + '.avi', cv2.VideoWriter_fourcc(*'mp4v'), 30, (1920, 1080), isColor=True)
  9. for file in tqdm(files):
  10. if file.endswith(".jpg"):
  11. file=os.path.join(dir,file)
  12. img=cv2.imread(file)
  13. #points=qrcoder.detect(img)
  14. #if points !=(False,None):
  15. #img_qrcode = cv2.drawContours(img, [np.int32(points)], 0, (0, 0, 255), 2)
  16. #else:
  17. #img_qrcode=img
  18. img_gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  19. img_binary=cv2.adaptiveThreshold(img_gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,5,0)
  20. ret,img_binary=cv2.threshold(img_gray,250,255,cv2.THRESH_BINARY_INV)
  21. kernel=np.ones((7,7))
  22. im_erode = cv2.erode(img_binary, kernel)
  23. im_erode = cv2.erode(im_erode, kernel)
  24. im_erode = cv2.erode(im_erode, kernel)
  25. im_dilate=cv2.dilate(im_erode,kernel)
  26. im_dilate = cv2.dilate(im_dilate, kernel)
  27. im_dilate = cv2.dilate(im_dilate, kernel)
  28. im_dilate = cv2.dilate(im_dilate, kernel)
  29. im_dilate = cv2.dilate(im_dilate, kernel)
  30. #img_open=cv2.morphologyEx(img_binary,cv2.MORPH_OPEN,kernel)
  31. img_blur=cv2.medianBlur(im_dilate,3)
  32. img_canny=cv2.Canny(img_blur,0,255)
  33. contours, hierarchy = cv2.findContours(img_blur, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
  34. max_area=0
  35. for contour in contours:
  36. lenth = cv2.arcLength(contour, True)
  37. area = cv2.contourArea(contour)
  38. if lenth>0 and area/lenth>max_area and 400<area< 1000000:
  39. contour_max=contour
  40. max_area=area/lenth
  41. img_contours = cv2.drawContours(img, contour_max, -1, (0, 0, 255),3)
  42. box=cv2.minAreaRect(contour_max)
  43. box=cv2.boxPoints(box)
  44. box=np.int0(box)
  45. #img_contours =cv2.drawContours(img,[box],0,(0,0,255),2)
  46. #cv2.imshow("raw",img)
  47. #cv2.imshow("qrcode",img_qrcode)
  48. #cv2.imshow("ray",img_gray)
  49. #cv2.imshow("binary",img_binary)
  50. #cv2.imshow("close",img_open)
  51. #cv2.imshow("blur",img_blur)
  52. #cv2.imshow("canny", img_canny)
  53. #cv2.imshow("rota", img_rota)
  54. #cv2.imshow("img_connect", img_connect)
  55. #cv2.imshow("img_contours", img_contours)
  56. #cv2.waitKey()
  57. writer.write(img_contours)
  58. writer.release()

 泛化性不太好,新到一批数据检测效果不佳,下一步考虑使用yolov5来检测。

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

闽ICP备14008679号