Skip to content

Instantly share code, notes, and snippets.

@grapeot
Last active November 1, 2022 07:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save grapeot/b2f3e481639dcde01178dbcdfa441c00 to your computer and use it in GitHub Desktop.
Save grapeot/b2f3e481639dcde01178dbcdfa441c00 to your computer and use it in GitHub Desktop.
ZWO AM5自动跟踪红色滑块
import zwoasi
import cv2
import numpy as np
from simple_pid import PID
from alpaca import Telescope
def initializeCamera():
zwoasi.init('./ASICamera2.dll')
camera = zwoasi.Camera(0)
controls = camera.get_controls()
camera.set_image_type(zwoasi.ASI_IMG_RGB24)
camera.start_video_capture()
camera.set_control_value(zwoasi.ASI_GAIN, 200)
camera.set_control_value(zwoasi.ASI_EXPOSURE, 25000)
return camera
def localizeObject(camera):
img = camera.capture_video_frame(timeout=1000)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
hue = hsv[:, :, 0]
sat = hsv[:, :, 1]
val = hsv[:, :, 2]
mask = (hue < 15) & (sat > 200) & (val > 50)
xv, yv = np.meshgrid(np.arange(img.shape[1]), np.arange(img.shape[0]))
xc = xv[mask].mean()
yc = yv[mask].mean()
return xc, yc, img
if __name__ == '__main__':
camera = initializeCamera()
T = Telescope('127.0.0.1:11111', 0) # Local Omni Simulator
pid = PID(2, 0.001, 0, setpoint=1920/2)
pid.sample_time = 0.1
oldtime = time()
try:
while True:
xc, yc, img = localizeObject(camera)
axis0 = pid(xc)
if not isnan(axis0) and abs(axis0) > 50:
T.moveaxis(1, -axis0 / 1000)
else:
T.abortslew()
oldtime = time()
except KeyboardInterrupt as e:
T.abortslew()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment