Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- #
- # author: Wappler Tibor
- #
- prg_name = "cam_cir_det.py" # camera circle detect --> x,y consumption
- #
- import cv2, numpy as np
- import picamera
- import sys,io
- import time
- from pkg_resources import require
- print("")
- print(prg_name,' output result :')
- print(require('picamera'))
- print('require detect ',require('picamera')[0].version)
- print("")
- #--- picamera resolutions:
- #wx,wy= 3280,2464 # mx219 maximum
- #wx,wy= 2592,1944 # ov5647 maximum
- #wx,wy= 2560,1920 #
- #wx,wy= 2304,1728 #
- #wx,wy= 2176,1632 #
- #wx,wy= 2048,1536 #
- #wx,wy= 1920,1440 #
- #wx,wy= 1792,1344 #
- #wx,wy= 1600,1216 #
- #wx,wy= 1536,1152 #
- wx,wy= 1280,960 #
- #wx,wy= 1024,768 #
- framerate = 30
- max_count = 100
- detect_count = 0
- not_detect_count = 0
- # detect x,y,r
- x = 0
- y = 0
- r = 0
- #--------------------------------
- def the_end(): # program finalize
- #--------------------------------
- print('')
- print('A ',prg_name,' end of running.')
- '''
- print('Regularly disconnect outputs.')
- LED_piros_nemeg()
- LED_sarga_nemeg()
- LED_zold_nemeg()
- GPIO.output(FET_gate, GPIO.HIGH)
- print('GPIO release legs, reset.')
- GPIO.cleanup()
- '''
- sys.exit(0)
- #-------- the_end() --------------------------------
- # -----------------------------------------------------
- while True: # the 'infinite' cycle of the main program
- # -----------------------------------------------------
- stream = io.BytesIO()
- with picamera.PiCamera() as camera:
- camera.resolution = wx,wy
- camera.color_effects = (128,128)
- camera.contrast = 100 # only works in good light
- if framerate > 10: # manual setup
- camera.framerate = framerate
- print('camera.contrast : ', camera.contrast,' adjusted' )
- print('camera.framerate : ', camera.framerate,' adjusted')
- print("")
- time.sleep(2)
- camera.start_preview(fullscreen=False, window=(0,40, 640,480))
- run_time_start = time.time() # for test measurement
- for foo in camera.capture_continuous(stream, format='bgr', use_video_port=True):
- stream.truncate()
- stream.seek(0)
- buff= stream.getbuffer()
- cam_image= np.frombuffer(buff, dtype=np.uint8).reshape(wy,wx,3)
- img_gray= cv2.cvtColor(cam_image, cv2.COLOR_BGR2GRAY)
- circleobj=cv2.HoughCircles(img_gray, cv2.HOUGH_GRADIENT,1,100,
- param1=100,
- param2=25,
- minRadius=4,
- maxRadius=18)
- # circle detect if circles is not None:
- if circleobj is None:
- not_detect_count = not_detect_count + 1
- del buff
- del cam_image
- stream.truncate(0)
- print('not_detect_count : ',not_detect_count)
- continue
- else:
- detect_count += 1
- # y,x you get a bad value
- y,x = img_gray.shape
- print('detect_count : ', detect_count)
- print('x : ',x,' ','y : ',y,' ','r : ', r)
- # max count finalize
- if detect_count >= max_count:
- print("")
- print('camera.revision : ',camera.revision)
- print('PiCamera.MAX_RESOLUTION : ',camera.MAX_RESOLUTION)
- print('camera.resolution ', camera.resolution,' adjusted' )
- print('camera.framerate : ', camera.framerate,' adjusted' )
- run_time_stop = time.time()
- run_time = run_time_stop - run_time_start
- print("")
- print('detect_count : ', detect_count,' pies')
- print('run_time : ', run_time )
- pic_sec = detect_count / run_time
- print('pic_sec : ', pic_sec )
- approximate_frame_rate = 60 / pic_sec
- print('approximate_framerate : ', approximate_frame_rate )
- print("")
- the_end() # program finalize
- del buff
- del cam_image
- stream.truncate(0)
- the_end() # program finalize
Advertisement
Add Comment
Please, Sign In to add comment