Guest User

Untitled

a guest
Sep 9th, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.05 KB | None | 0 0
  1. import socket
  2. import os
  3. from picamera2 import Picamera2, Preview
  4. import sys
  5.  
  6.  
  7. def save_image(host, port):
  8.     tuning = Picamera2.load_tuning_file("/home/USER/test_python/imx219_70d.json")
  9.     picam2 = Picamera2(tuning=tuning)
  10.     camera_config = picam2.create_still_configuration(
  11.         main={"size": (3280, 2464)},
  12.         controls={
  13.             "AwbEnable": False,
  14.             "ColourGains": (1.0, 1.0)
  15.         }
  16.     )
  17.     picam2.configure(camera_config)
  18.     picam2.start_preview(Preview.NULL)
  19.  
  20.     server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  21.     server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  22.     server_socket.bind((host, port))
  23.     server_socket.listen()
  24.  
  25.     while True:
  26.         print("Waiting for a connection...")
  27.         try:
  28.             connection, client_address = server_socket.accept()
  29.         except Exception as e:
  30.             print("An error occurred while accepting a connection: ", e)
  31.             continue
  32.         print(connection)
  33.         print("Connection from:", client_address)
  34.  
  35.         if os.path.exists("/home/USER/test_python/captured_image.jpg"):
  36.             # In order to make "transfer_client" fail later, in case of a capture fail
  37.             os.remove("/home/USER/test_python/captured_image.jpg")
  38.  
  39.         capture_success = True
  40.         try:
  41.             picam2.start_and_capture_file(
  42.                 "/home/USER/test_python/captured_image.jpg", show_preview=False
  43.             )
  44.         except Exception as e:
  45.             print("An error occurred while capturing an image:", e)
  46.             capture_success = False
  47.  
  48.         with connection:
  49.             try:
  50.                 connection.send(
  51.                     b"success" if capture_success else b"fail"
  52.                 )
  53.             except Exception as e:
  54.                 print("An error occurred while sending result: ", e)
  55.                 continue
  56.  
  57.  
  58. def main(argv):
  59.     host = argv[1]
  60.     port = int(argv[2])
  61.     print(host, port)
  62.     save_image(host, port)
  63.  
  64.  
  65. if __name__ == "__main__":
  66.     main(sys.argv)
  67.  
Advertisement
Add Comment
Please, Sign In to add comment