virtualm

cam_cir_det.py

Jul 2nd, 2020
1,040
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.55 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #
  4. # author:   Wappler Tibor
  5. #    
  6. prg_name = "cam_cir_det.py" # camera circle detect --> x,y consumption
  7. #
  8.  
  9. import cv2, numpy as np
  10. import picamera
  11. import sys,io
  12. import time
  13. from pkg_resources import require
  14.  
  15. print("")
  16. print(prg_name,' output result :')
  17. print(require('picamera'))
  18. print('require detect ',require('picamera')[0].version)
  19. print("")
  20.  
  21.      
  22. #--- picamera resolutions:
  23. #wx,wy= 3280,2464 # mx219 maximum
  24. #wx,wy= 2592,1944 # ov5647 maximum
  25. #wx,wy= 2560,1920 #
  26. #wx,wy= 2304,1728 #
  27. #wx,wy= 2176,1632 #
  28. #wx,wy= 2048,1536 #
  29. #wx,wy= 1920,1440 #
  30. #wx,wy= 1792,1344 #
  31. #wx,wy= 1600,1216 #
  32. #wx,wy= 1536,1152 #
  33. wx,wy= 1280,960  #
  34. #wx,wy= 1024,768  #
  35.  
  36. framerate = 30
  37. max_count = 100
  38. detect_count = 0                  
  39. not_detect_count = 0
  40.  
  41. # detect x,y,r
  42. x  = 0
  43. y = 0
  44. r = 0
  45.  
  46. #--------------------------------
  47. def the_end(): # program finalize
  48. #--------------------------------
  49.     print('')
  50.     print('A ',prg_name,' end of running.')
  51.     '''
  52.    print('Regularly disconnect outputs.')
  53.    LED_piros_nemeg()
  54.    LED_sarga_nemeg()
  55.    LED_zold_nemeg()
  56.    GPIO.output(FET_gate, GPIO.HIGH)
  57.    
  58.    print('GPIO release legs, reset.')
  59.    GPIO.cleanup()
  60.    '''
  61.     sys.exit(0)
  62. #-------- the_end() --------------------------------
  63.  
  64.  
  65. # -----------------------------------------------------
  66. while True: # the 'infinite' cycle of the main program
  67. # -----------------------------------------------------  
  68.        
  69.     stream = io.BytesIO()
  70.     with picamera.PiCamera() as camera:
  71.         camera.resolution = wx,wy        
  72.         camera.color_effects = (128,128)
  73.         camera.contrast = 100 # only works in good light
  74.         if framerate > 10: # manual setup
  75.             camera.framerate = framerate
  76.         print('camera.contrast : ', camera.contrast,' adjusted' )
  77.         print('camera.framerate : ', camera.framerate,' adjusted')
  78.         print("")
  79.        
  80.         time.sleep(2)
  81.        
  82.         camera.start_preview(fullscreen=False, window=(0,40, 640,480))
  83.                
  84.         run_time_start = time.time() # for test measurement
  85.        
  86.         for foo in camera.capture_continuous(stream, format='bgr', use_video_port=True):
  87.             stream.truncate()
  88.             stream.seek(0)
  89.             buff= stream.getbuffer()
  90.             cam_image= np.frombuffer(buff, dtype=np.uint8).reshape(wy,wx,3)
  91.             img_gray= cv2.cvtColor(cam_image, cv2.COLOR_BGR2GRAY)
  92.            
  93.             circleobj=cv2.HoughCircles(img_gray, cv2.HOUGH_GRADIENT,1,100,
  94.                                                             param1=100,
  95.                                                             param2=25,
  96.                                                             minRadius=4,
  97.                                                             maxRadius=18)
  98.            
  99.             # circle detect if circles is not None:
  100.             if circleobj is None:
  101.                 not_detect_count = not_detect_count + 1
  102.                 del buff
  103.                 del cam_image
  104.                 stream.truncate(0)
  105.                 print('not_detect_count : ',not_detect_count)
  106.                 continue
  107.             else:
  108.                 detect_count += 1
  109.                 # y,x you get a bad value
  110.                 y,x = img_gray.shape
  111.                 print('detect_count : ', detect_count)
  112.                 print('x : ',x,' ','y : ',y,' ','r : ', r)
  113.                    
  114.             #               max count finalize      
  115.             if detect_count >= max_count:
  116.                 print("")
  117.                 print('camera.revision : ',camera.revision)
  118.                 print('PiCamera.MAX_RESOLUTION : ',camera.MAX_RESOLUTION)
  119.                 print('camera.resolution ', camera.resolution,' adjusted' )
  120.                 print('camera.framerate : ', camera.framerate,' adjusted' )
  121.            
  122.                 run_time_stop = time.time()
  123.                 run_time = run_time_stop - run_time_start
  124.                 print("")
  125.                 print('detect_count : ', detect_count,' pies')
  126.                 print('run_time : ', run_time )
  127.                 pic_sec = detect_count / run_time
  128.                 print('pic_sec : ', pic_sec )
  129.                 approximate_frame_rate = 60 / pic_sec
  130.                 print('approximate_framerate : ', approximate_frame_rate )
  131.                 print("")
  132.                 the_end() # program finalize
  133.                    
  134.             del buff  
  135.             del cam_image
  136.             stream.truncate(0)
  137.                  
  138. the_end() # program finalize
Advertisement
Add Comment
Please, Sign In to add comment