Guest User

Untitled

a guest
May 19th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import os
  4. import time
  5. import smbus2 as smbus
  6. import matplotlib.pyplot as plt
  7. from scipy.optimize import leastsq
  8. from imutils.video import VideoStream
  9. import imutils
  10.  
  11. try:
  12.     import picamera
  13.     from picamera.array import PiRGBArray
  14. except:
  15.     sys.exit(0)
  16.    
  17. TestCount = 200
  18. StartTime = 0
  19. FocalDistSpace = 40
  20. ThresholdCount = 2
  21.    
  22.  
  23. def capturepic(focal_dist, pic_name):
  24.     focusing(int(focal_dist))
  25.     time.sleep(0.3)
  26.     camera.capture(pic_name)
  27.     #frame = camera.read()
  28.     #cv2.imwrite(pic_name, frame)
  29.  
  30. def focusing(val):
  31.     i2c = smbus.SMBus(1)
  32.     addr = 0x0c
  33.     reg = (((val << 4) & 0x3ff0) >> 8) & 0x3f
  34.     value = ((val << 4) & 0x3ff0) & 0xf0
  35.     try:
  36.         i2c.write_byte_data(addr, reg, value)
  37.     except:
  38.         pass
  39.     i2c.close()
  40.     #value = (val << 4) & 0x3ff0
  41.     #data1 = (value >> 8) & 0x3f
  42.     #data2 = value & 0xf0
  43.     #os.system("i2cset -y 1 0x0c %d %d" % (data1, data2))
  44.  
  45. def laplacian_var(camera):
  46.     rawCapture = PiRGBArray(camera)
  47.     camera.capture(rawCapture,format="bgr", use_video_port=True)
  48.     img = rawCapture.array
  49.     rawCapture.truncate(0)
  50.     img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
  51.     return cv2.Laplacian(img_gray, cv2.CV_64F).var()
  52.  
  53. if __name__ == "__main__":
  54.    
  55.     camera = picamera.PiCamera()
  56.     #camera.start_preview()
  57.     camera.resolution = (640,480)
  58.     time.sleep(2)
  59.  
  60.     camera.shutter_speed = camera.exposure_speed
  61.     camera.exposure_mode = 'off'
  62.     g = camera.awb_gains
  63.     camera.awb_mode = 'off'
  64.     camera.awb_gains = g
  65.    
  66.     time.sleep(2)
  67.    
  68.     for i in range (TestCount):        
  69.        
  70.         print("-----Photo " + str(i) + "-----")
  71.         max_index = 10
  72.         max_value = 0.0
  73.         last_value = 0.0
  74.         dec_count = 0
  75.         top_index = 0
  76.         focal_distance = 200
  77.  
  78.         StartTime = time.time()
  79.  
  80.         RecordX = []
  81.         RecordY = []
  82.  
  83.         while True:
  84.             focusing(focal_distance)
  85.             val = laplacian_var(camera)
  86.  
  87.             if val > max_value:
  88.                 max_index = focal_distance
  89.                 max_value = val
  90.  
  91.             if val < last_value:
  92.                 dec_count += 1
  93.             else:
  94.                 dec_count = 0
  95.  
  96.             if dec_count > ThresholdCount:
  97.                 break
  98.  
  99.             focal_distance += FocalDistSpace
  100.  
  101.             if focal_distance > 1000:
  102.                 break
  103.             #time.sleep(0.2)
  104.  
  105.         print("TopPoint = (%d , %lf)" %(max_index, max_value))
  106.         capturepic(max_index, "OriginAlgo_" + str(i) + ".png")
  107.  
  108.  
  109.     #camera.stop_preview()  
  110.     camera.exposure_mode = 'auto'
  111.     camera.awb_mode = 'auto'
  112.     camera.close()
Add Comment
Please, Sign In to add comment