diff --git a/Motorized_Focus_Camera/python/Motorized_Focus_Camera_Preview.py b/Motorized_Focus_Camera/python/Motorized_Focus_Camera_Preview.py index 6122e4f..10fcebc 100644 --- a/Motorized_Focus_Camera/python/Motorized_Focus_Camera_Preview.py +++ b/Motorized_Focus_Camera/python/Motorized_Focus_Camera_Preview.py @@ -2,8 +2,9 @@ import os import time import sys -import thread +import _thread as thread from ctypes import * + keyboard = CDLL('./lib/libarducam_keyboard.so') arducam_vcm =CDLL('./lib/libarducam_vcm.so') UP = 1 @@ -11,8 +12,11 @@ SAVE = 115 focus_val = 512; step = 10 + +CAMERA_NUMBER = 0 + def run_camera(name): - os.system("raspistill -t 0") + os.system(f"raspistill -t 0 -cs {CAMERA_NUMBER}") if __name__ == "__main__": thread.start_new_thread(run_camera, ("run_camera",)) #vcm init @@ -22,6 +26,8 @@ def run_camera(name): # preview=camera.start_preview() #set windows size # preview.window=(0,0,800,600) + + arducam_vcm.choose_channel(CAMERA_NUMBER) print("Please press up and down to adjust focus.") while True: keyVal = keyboard.processKeyEvent() diff --git a/Motorized_Focus_Camera/python/multi_cameras_auto_focus.py b/Motorized_Focus_Camera/python/multi_cameras_auto_focus.py index 2cf1fa5..3dd47cf 100644 --- a/Motorized_Focus_Camera/python/multi_cameras_auto_focus.py +++ b/Motorized_Focus_Camera/python/multi_cameras_auto_focus.py @@ -1,133 +1,131 @@ import cv2 #sudo apt-get install python-opencv -import numpy as py import os import time from ctypes import * -cameraNum = 2 + #load arducam shared object file arducam_vcm= CDLL('./lib/libarducam_vcm.so') try: - import picamera - from picamera.array import PiRGBArray + import picamera + from picamera.array import PiRGBArray except: - sys.exit(0) - + sys.exit(0) + def focusing(val): - arducam_vcm.vcm_write(val) - #print("focus value: {}".format(val)) - + arducam_vcm.vcm_write(val) + #print("focus value: {}".format(val)) + def sobel(img): - img_gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY) - img_sobel = cv2.Sobel(img_gray,cv2.CV_16U,1,1) - return cv2.mean(img_sobel)[0] + img_gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY) + img_sobel = cv2.Sobel(img_gray,cv2.CV_16U,1,1) + return cv2.mean(img_sobel)[0] def laplacian(img): - img_gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY) - img_sobel = cv2.Laplacian(img_gray,cv2.CV_16U) - return cv2.mean(img_sobel)[0] - + img_gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY) + img_sobel = cv2.Laplacian(img_gray,cv2.CV_16U) + return cv2.mean(img_sobel)[0] + def calculation(camera): - rawCapture = PiRGBArray(camera) - camera.capture(rawCapture,format="bgr", use_video_port=True) - image = rawCapture.array - rawCapture.truncate(0) - return laplacian(image) - + rawCapture = PiRGBArray(camera) + camera.capture(rawCapture,format="bgr", use_video_port=True) + image = rawCapture.array + rawCapture.truncate(0) + return laplacian(image) + if __name__ == "__main__": #vcm init - arducam_vcm.vcm_init() + arducam_vcm.vcm_init() #open camera - camera = picamera.PiCamera() - - #open camera preview - camera.start_preview() - #set camera resolution to 640x480(Small resolution for faster speeds.) - camera.resolution = (640, 480) - time.sleep(0.1) - print("Start focusing 1") - max_index = 10 - max_value = 0.0 - last_value = 0.0 - dec_count = 0 - focal_distance = 10 - #choose cmaera A - arducam_vcm.choose_channel(0) - while True: - #Adjust focus - focusing(focal_distance) - #Take image and calculate image clarity - val = calculation(camera) - #Find the maximum image clarity - if val > max_value: - max_index = focal_distance - max_value = val - #If the image clarity starts to decrease - if val < last_value: - dec_count += 1 - else: - dec_count = 0 - #Image clarity is reduced by six consecutive frames - if dec_count > 6: - break - last_value = val - #Increase the focal distance - focal_distance += 15 - if focal_distance > 1000: - break + camera = picamera.PiCamera() + + #open camera preview + camera.start_preview() + #set camera resolution to 640x480(Small resolution for faster speeds.) + camera.resolution = (640, 480) + time.sleep(0.1) + print("Start focusing 1") + max_index = 10 + max_value = 0.0 + last_value = 0.0 + dec_count = 0 + focal_distance = 10 + #choose cmaera A + arducam_vcm.choose_channel(0) + while True: + # adjust focus + focusing(focal_distance) + #Take image and calculate image clarity + val = calculation(camera) + #Find the maximum image clarity + if val > max_value: + max_index = focal_distance + max_value = val + #If the image clarity starts to decrease + if val < last_value: + dec_count += 1 + else: + dec_count = 0 + #Image clarity is reduced by six consecutive frames + if dec_count > 6: + break + last_value = val + #Increase the focal distance + focal_distance += 15 + if focal_distance > 1000: + break #Adjust focus to the best - focusing(max_index) - time.sleep(1) - #set camera resolution to 2592x1944 - camera.resolution = (1920,1080) - #save image to file. - camera.capture("test_A.jpg") - print("max index = %d,max value = %lf" % (max_index,max_value)) - print("Start focusing 2") - max_index = 10 - max_value = 0.0 - last_value = 0.0 - dec_count = 0 - focal_distance = 10 - camera.resolution = (640, 480) - #choose cmaera A - arducam_vcm.choose_channel(1) - while True: - #Adjust focus - focusing(focal_distance) - #Take image and calculate image clarity - val = calculation(camera) - #Find the maximum image clarity - if val > max_value: - max_index = focal_distance - max_value = val - #If the image clarity starts to decrease - if val < last_value: - dec_count += 1 - else: - dec_count = 0 - #Image clarity is reduced by six consecutive frames - if dec_count > 6: - break - last_value = val - #Increase the focal distance - focal_distance += 15 - if focal_distance > 1000: - break + focusing(max_index) + time.sleep(1) + #set camera resolution to 2592x1944 + camera.resolution = (1920,1080) + #save image to file. + camera.capture("test_A.jpg") + print("max index = %d,max value = %lf" % (max_index,max_value)) + print("Start focusing 2") + max_index = 10 + max_value = 0.0 + last_value = 0.0 + dec_count = 0 + focal_distance = 10 + camera.resolution = (640, 480) + #choose cmaera A + arducam_vcm.choose_channel(1) + while True: + # adjust focus + focusing(focal_distance) + #Take image and calculate image clarity + val = calculation(camera) + #Find the maximum image clarity + if val > max_value: + max_index = focal_distance + max_value = val + #If the image clarity starts to decrease + if val < last_value: + dec_count += 1 + else: + dec_count = 0 + #Image clarity is reduced by six consecutive frames + if dec_count > 6: + break + last_value = val + #Increase the focal distance + focal_distance += 15 + if focal_distance > 1000: + break + #Adjust focus to the best - focusing(max_index) - time.sleep(1) - #set camera resolution to 2592x1944 - camera.resolution = (1920,1080) - #save image to file. - camera.capture("test_B.jpg") - print("max index = %d,max value = %lf" % (max_index,max_value)) - - - #while True: - # time.sleep(1) - camera.stop_preview() - camera.close() - - + focusing(max_index) + time.sleep(1) + #set camera resolution to 2592x1944 + camera.resolution = (1920,1080) + #save image to file. + camera.capture("test_B.jpg") + print("max index = %d,max value = %lf" % (max_index,max_value)) + + + #while True: + #time.sleep(1) + camera.stop_preview() + camera.close()