Advertisement
virtualm

cam_cir_det_v1.2.py

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