python OpenCV对象检测与视频文件保存

nkhmeac6  于 2022-10-30  发布在  Python
关注(0)|答案(2)|浏览(244)

我想进行对象检测并保存视频,但视频仅保存了6kb或0kb,无法播放
如果没有这条线

  1. x, y, width, height, area = stats[index]

它将被保存
你知道为什么吗有解决的办法吗?

  1. import cv2
  2. import time
  3. import numpy as np
  4. cap = cv2.VideoCapture("rtsp://admin:admin@128.1.1.110:554")
  5. width = int(cap.get(3))
  6. height = int(cap.get(4))
  7. fcc = cv2.VideoWriter_fourcc(*'XVID')
  8. recording = False
  9. fgbg = cv2.createBackgroundSubtractorMOG2(varThreshold=200, detectShadows=0)
  10. while(1):
  11. ret, frame = cap.read()
  12. hms = time.strftime('%H_%M_%S', time.localtime())
  13. fgmask = fgbg.apply(frame)
  14. nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(fgmask)
  15. for index, centroid in enumerate(centroids):
  16. if stats[index][0] == 0 and stats[index][1] == 0:
  17. continue
  18. if np.any(np.isnan(centroid)):
  19. continue
  20. x, y, width, height, area = stats[index]
  21. centerX, centerY = int(centroid[0]), int(centroid[1])
  22. if area > 200:
  23. cv2.circle(frame, (centerX, centerY), 1, (0, 255, 0), 2)
  24. cv2.rectangle(frame, (x, y), (x + width, y + height), (0, 0, 255))
  25. cv2.putText(frame, str(area), (centerX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255))
  26. cv2.imshow('frame', frame)
  27. k = cv2.waitKey(1) & 0xff
  28. if k == ord('r') and recording is False:
  29. path = 'test_' + str(hms) + '.avi'
  30. print('recording start')
  31. writer = cv2.VideoWriter(path, fcc, 30.0, (width, height))
  32. recording = True
  33. if recording:
  34. writer.write(frame)
  35. if k == ord('e'):
  36. print('recording end')
  37. recording = False
  38. writer.release()
  39. cap.release()
  40. cv2.destroyAllWindows()
643ylb08

643ylb081#

我想这会解决你的问题

  1. # importing the module
  2. import cv2
  3. import numpy as np
  4. # reading the vedio
  5. source = cv2.VideoCapture(0) // add your URL insed of "0"
  6. # We need to set resolutions.
  7. # so, convert them from float to integer.
  8. frame_width = int(source.get(3))
  9. frame_height = int(source.get(4))
  10. recording = False
  11. fcc = cv2.VideoWriter_fourcc(*'XVID')
  12. size = (frame_width, frame_height)
  13. fgbg = cv2.createBackgroundSubtractorMOG2(varThreshold=200, detectShadows=0)
  14. result = cv2.VideoWriter('output.avi', fcc, 30, size)
  15. # running the loop
  16. while True:
  17. # extracting the frames
  18. ret, frame = source.read()
  19. fgmask = fgbg.apply(frame)
  20. nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(fgmask)
  21. for index, centroid in enumerate(centroids):
  22. if stats[index][0] == 0 and stats[index][1] == 0:
  23. continue
  24. if np.any(np.isnan(centroid)):
  25. continue
  26. x, y, width, height, area = stats[index]
  27. centerX, centerY = int(centroid[0]), int(centroid[1])
  28. if area > 200:
  29. cv2.circle(frame, (centerX, centerY), 1, (0, 255, 0), 2)
  30. cv2.rectangle(frame, (x, y), (x + width, y + height), (0, 0, 255))
  31. cv2.putText(frame, str(area), (centerX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255))
  32. # displaying the video
  33. cv2.imshow("Live", frame)
  34. k = cv2.waitKey(1) & 0xff
  35. if k == ord('r') and recording is False:
  36. print('recording start')
  37. recording = True
  38. if recording:
  39. result.write(frame)
  40. if k == ord('e'):
  41. print('recording end')
  42. recording = False
  43. result.release()
  44. # closing the window
  45. cv2.destroyAllWindows()
  46. source.release()

但很遗憾,我不能hms与输出文件名。那可以试试你自己,如果有帮助的话这对你给予👍

展开查看全部
qaxu7uf2

qaxu7uf22#

实际上,你需要删除一些代码。

  1. cv2.imshow('MultiTracker', frame)
  2. # quit on ESC button
  3. if cv2.waitKey(1) & 0xFF == 27: # Esc pressed
  4. break
  5. # k = cv2.waitKey(1) & 0xff
  6. #if k == ord('r') and recording is False:
  7. # print('recording start')
  8. # recording = True
  9. #if recording:
  10. result.write(frame)
  11. #if k == ord('e'):
  12. # print('recording end')
  13. # recording = False
  14. # result.release()
  15. result.release()
  16. cv2.destroyAllWindows()
  17. cap.release()

它对我很有效,它是6KB的原因是你开始写,但不附加帧到输出avi文件。

展开查看全部

相关问题