Changed color camera back to color, set all cameras to 20ms shutter speed
This commit is contained in:
parent
35ddf9f844
commit
413590d1a2
@ -11,7 +11,7 @@ TARGET_NUM_CAMS = 3
|
|||||||
DEFAULT_W = 1280
|
DEFAULT_W = 1280
|
||||||
DEFAULT_H = 720
|
DEFAULT_H = 720
|
||||||
|
|
||||||
# --- PART 1: DETECTION ---
|
# --- PART 1: DETECTION (Unchanged) ---
|
||||||
def scan_connected_cameras():
|
def scan_connected_cameras():
|
||||||
print("--- Scanning for Basler Cameras ---")
|
print("--- Scanning for Basler Cameras ---")
|
||||||
detection_script = """
|
detection_script = """
|
||||||
@ -23,45 +23,58 @@ try:
|
|||||||
if not devices:
|
if not devices:
|
||||||
print("NONE")
|
print("NONE")
|
||||||
else:
|
else:
|
||||||
serials = [d.GetSerialNumber() for d in devices]
|
results = []
|
||||||
cam = pylon.InstantCamera(tl_factory.CreateDevice(devices[0]))
|
for i in range(len(devices)):
|
||||||
cam.Open()
|
cam = pylon.InstantCamera(tl_factory.CreateDevice(devices[i]))
|
||||||
try:
|
cam.Open()
|
||||||
cam.BinningHorizontal.Value = 2
|
serial = cam.GetDeviceInfo().GetSerialNumber()
|
||||||
cam.BinningVertical.Value = 2
|
model = cam.GetDeviceInfo().GetModelName()
|
||||||
|
is_color = model.endswith("c") or "Color" in model
|
||||||
w = cam.Width.GetValue()
|
w = cam.Width.GetValue()
|
||||||
h = cam.Height.GetValue()
|
h = cam.Height.GetValue()
|
||||||
cam.BinningHorizontal.Value = 1
|
binning = 0
|
||||||
cam.BinningVertical.Value = 1
|
try:
|
||||||
supported = 1
|
cam.BinningHorizontal.Value = 2
|
||||||
except:
|
cam.BinningVertical.Value = 2
|
||||||
w = cam.Width.GetValue()
|
cam.BinningHorizontal.Value = 1
|
||||||
h = cam.Height.GetValue()
|
cam.BinningVertical.Value = 1
|
||||||
supported = 0
|
binning = 1
|
||||||
cam.Close()
|
except: pass
|
||||||
print(f"{','.join(serials)}|{w}|{h}|{supported}")
|
current_fmt = cam.PixelFormat.GetValue()
|
||||||
except Exception:
|
cam.Close()
|
||||||
print("NONE")
|
results.append(f"{serial}:{w}:{h}:{binning}:{1 if is_color else 0}:{model}:{current_fmt}")
|
||||||
|
print("|".join(results))
|
||||||
|
except Exception: print("NONE")
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
result = subprocess.run([sys.executable, "-c", detection_script], capture_output=True, text=True)
|
result = subprocess.run([sys.executable, "-c", detection_script], capture_output=True, text=True)
|
||||||
output = result.stdout.strip()
|
output = result.stdout.strip()
|
||||||
if "NONE" in output or not output:
|
if "NONE" in output or not output: return []
|
||||||
return [], DEFAULT_W, DEFAULT_H, False
|
camera_list = []
|
||||||
parts = output.split('|')
|
entries = output.split('|')
|
||||||
return parts[0].split(','), int(parts[1]), int(parts[2]), (parts[3] == '1')
|
for entry in entries:
|
||||||
except: return [], DEFAULT_W, DEFAULT_H, False
|
parts = entry.split(':')
|
||||||
|
camera_list.append({
|
||||||
|
"serial": parts[0], "width": int(parts[1]), "height": int(parts[2]),
|
||||||
|
"binning": (parts[3] == '1'), "is_color": (parts[4] == '1'), "model": parts[5]
|
||||||
|
})
|
||||||
|
return camera_list
|
||||||
|
except: return []
|
||||||
|
|
||||||
DETECTED_SERIALS, CAM_W, CAM_H, BINNING_SUPPORTED = scan_connected_cameras()
|
DETECTED_CAMS = scan_connected_cameras()
|
||||||
ACTUAL_CAMS_COUNT = len(DETECTED_SERIALS)
|
ACTUAL_CAMS_COUNT = len(DETECTED_CAMS)
|
||||||
|
|
||||||
# --- RESOLUTION & LAYOUT ---
|
# --- RESOLUTION LOGIC ---
|
||||||
INTERNAL_WIDTH = 1280
|
|
||||||
if ACTUAL_CAMS_COUNT > 0:
|
if ACTUAL_CAMS_COUNT > 0:
|
||||||
scale = INTERNAL_WIDTH / CAM_W
|
MASTER_W = DETECTED_CAMS[0]['width']
|
||||||
INTERNAL_HEIGHT = int(CAM_H * scale)
|
MASTER_H = DETECTED_CAMS[0]['height']
|
||||||
else:
|
else:
|
||||||
INTERNAL_HEIGHT = 720
|
MASTER_W = DEFAULT_W
|
||||||
|
MASTER_H = DEFAULT_H
|
||||||
|
|
||||||
|
INTERNAL_WIDTH = 1280
|
||||||
|
scale = INTERNAL_WIDTH / MASTER_W
|
||||||
|
INTERNAL_HEIGHT = int(MASTER_H * scale)
|
||||||
if INTERNAL_HEIGHT % 2 != 0: INTERNAL_HEIGHT += 1
|
if INTERNAL_HEIGHT % 2 != 0: INTERNAL_HEIGHT += 1
|
||||||
|
|
||||||
WEB_WIDTH = 1280
|
WEB_WIDTH = 1280
|
||||||
@ -70,7 +83,9 @@ scale_tiled = WEB_WIDTH / total_source_width
|
|||||||
WEB_HEIGHT = int(INTERNAL_HEIGHT * scale_tiled)
|
WEB_HEIGHT = int(INTERNAL_HEIGHT * scale_tiled)
|
||||||
if WEB_HEIGHT % 2 != 0: WEB_HEIGHT += 1
|
if WEB_HEIGHT % 2 != 0: WEB_HEIGHT += 1
|
||||||
|
|
||||||
print(f"LAYOUT: {TARGET_NUM_CAMS} Slots | Detected: {ACTUAL_CAMS_COUNT} Cams")
|
print(f"LAYOUT: {TARGET_NUM_CAMS} Slots | Detected: {ACTUAL_CAMS_COUNT}")
|
||||||
|
for c in DETECTED_CAMS:
|
||||||
|
print(f" - Cam {c['serial']} ({c['model']}): {'COLOR' if c['is_color'] else 'MONO'}")
|
||||||
|
|
||||||
# --- FLASK & GSTREAMER ---
|
# --- FLASK & GSTREAMER ---
|
||||||
import gi
|
import gi
|
||||||
@ -107,7 +122,6 @@ class GStreamerPipeline(threading.Thread):
|
|||||||
if not sample: return Gst.FlowReturn.ERROR
|
if not sample: return Gst.FlowReturn.ERROR
|
||||||
|
|
||||||
frame_count += 1
|
frame_count += 1
|
||||||
# Calculate FPS every 30 frames
|
|
||||||
if frame_count % 30 == 0:
|
if frame_count % 30 == 0:
|
||||||
elapsed = time.time() - start_time
|
elapsed = time.time() - start_time
|
||||||
current_fps = 30 / elapsed if elapsed > 0 else 0
|
current_fps = 30 / elapsed if elapsed > 0 else 0
|
||||||
@ -125,52 +139,80 @@ class GStreamerPipeline(threading.Thread):
|
|||||||
return Gst.FlowReturn.OK
|
return Gst.FlowReturn.OK
|
||||||
|
|
||||||
def build_pipeline(self):
|
def build_pipeline(self):
|
||||||
# 1. CAMERA SETTINGS
|
|
||||||
# Note: We run cameras at 60 FPS for internal stability
|
|
||||||
cam_settings = (
|
|
||||||
"cam::TriggerMode=Off "
|
|
||||||
"cam::AcquisitionFrameRateEnable=true cam::AcquisitionFrameRate=60.0 "
|
|
||||||
"cam::ExposureAuto=Off "
|
|
||||||
"cam::ExposureTime=20000.0 "
|
|
||||||
"cam::GainAuto=Continuous "
|
|
||||||
"cam::DeviceLinkThroughputLimitMode=Off "
|
|
||||||
)
|
|
||||||
if BINNING_SUPPORTED:
|
|
||||||
cam_settings += "cam::BinningHorizontal=2 cam::BinningVertical=2 "
|
|
||||||
|
|
||||||
sources_str = ""
|
sources_str = ""
|
||||||
|
|
||||||
for i in range(TARGET_NUM_CAMS):
|
for i in range(TARGET_NUM_CAMS):
|
||||||
if i < len(DETECTED_SERIALS):
|
if i < len(DETECTED_CAMS):
|
||||||
# --- REAL CAMERA SOURCE ---
|
cam_info = DETECTED_CAMS[i]
|
||||||
serial = DETECTED_SERIALS[i]
|
serial = cam_info['serial']
|
||||||
print(f"Slot {i}: Linking Camera {serial}")
|
is_color = cam_info['is_color']
|
||||||
|
|
||||||
|
print(f"Slot {i}: Linking {serial} [{'Color' if is_color else 'Mono'}]")
|
||||||
|
|
||||||
|
# --- 1. BASE SETTINGS (Common) ---
|
||||||
|
# We DISABLE Throughput Limit to allow high bandwidth
|
||||||
|
base_settings = (
|
||||||
|
f"pylonsrc device-serial-number={serial} "
|
||||||
|
"cam::TriggerMode=Off "
|
||||||
|
"cam::AcquisitionFrameRateEnable=true cam::AcquisitionFrameRate=60.0 "
|
||||||
|
"cam::DeviceLinkThroughputLimitMode=Off "
|
||||||
|
)
|
||||||
|
|
||||||
|
# Pre-scaler
|
||||||
pre_scale = (
|
pre_scale = (
|
||||||
"nvvideoconvert compute-hw=1 ! "
|
"nvvideoconvert compute-hw=1 ! "
|
||||||
f"video/x-raw(memory:NVMM), format=NV12, width={INTERNAL_WIDTH}, height={INTERNAL_HEIGHT}, framerate=60/1 ! "
|
f"video/x-raw(memory:NVMM), format=NV12, width={INTERNAL_WIDTH}, height={INTERNAL_HEIGHT}, framerate=60/1 ! "
|
||||||
)
|
)
|
||||||
|
|
||||||
source = (
|
if is_color:
|
||||||
f"pylonsrc device-serial-number={serial} {cam_settings} ! "
|
# --- 2A. COLOR SETTINGS (High Speed) ---
|
||||||
"video/x-raw,format=GRAY8 ! "
|
# FIX: Force ExposureTime=20000.0 (20ms) even for Color.
|
||||||
"videoconvert ! "
|
# If we leave it on Auto, it will slow down the Mono cameras.
|
||||||
"video/x-raw,format=I420 ! "
|
# We rely on 'GainAuto' to make the image bright enough.
|
||||||
"nvvideoconvert compute-hw=1 ! "
|
color_settings = (
|
||||||
"video/x-raw(memory:NVMM) ! "
|
f"{base_settings} "
|
||||||
f"{pre_scale}"
|
"cam::ExposureAuto=Off cam::ExposureTime=20000.0 "
|
||||||
f"m.sink_{i} "
|
"cam::GainAuto=Continuous "
|
||||||
)
|
"cam::Width=1920 cam::Height=1080 cam::OffsetX=336 cam::OffsetY=484 "
|
||||||
|
"cam::PixelFormat=BayerBG8 " # Force Format
|
||||||
|
)
|
||||||
|
|
||||||
|
source = (
|
||||||
|
f"{color_settings} ! "
|
||||||
|
"bayer2rgb ! " # Debayer
|
||||||
|
"videoconvert ! "
|
||||||
|
"video/x-raw,format=RGBA ! "
|
||||||
|
"nvvideoconvert compute-hw=1 ! "
|
||||||
|
f"video/x-raw(memory:NVMM), format=NV12 ! "
|
||||||
|
f"{pre_scale}"
|
||||||
|
f"m.sink_{i} "
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
# --- 2B. MONO SETTINGS (High Speed) ---
|
||||||
|
# Force ExposureTime=20000.0
|
||||||
|
mono_settings = (
|
||||||
|
f"{base_settings} "
|
||||||
|
"cam::ExposureAuto=Off cam::ExposureTime=20000.0 "
|
||||||
|
"cam::GainAuto=Continuous "
|
||||||
|
)
|
||||||
|
if cam_info['binning']:
|
||||||
|
mono_settings += "cam::BinningHorizontal=2 cam::BinningVertical=2 "
|
||||||
|
|
||||||
|
source = (
|
||||||
|
f"{mono_settings} ! "
|
||||||
|
"video/x-raw,format=GRAY8 ! "
|
||||||
|
"videoconvert ! "
|
||||||
|
"video/x-raw,format=I420 ! "
|
||||||
|
"nvvideoconvert compute-hw=1 ! "
|
||||||
|
f"video/x-raw(memory:NVMM), format=NV12 ! "
|
||||||
|
f"{pre_scale}"
|
||||||
|
f"m.sink_{i} "
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
# --- DISCONNECTED PLACEHOLDER ---
|
# --- DISCONNECTED PLACEHOLDER ---
|
||||||
print(f"Slot {i}: Creating Placeholder (Synchronized)")
|
|
||||||
|
|
||||||
# FIX 1: Add 'videorate' to enforce strict timing on the fake source
|
|
||||||
# This prevents the placeholder from running too fast/slow and jittering the muxer
|
|
||||||
|
|
||||||
source = (
|
source = (
|
||||||
f"videotestsrc pattern=black is-live=true ! "
|
f"videotestsrc pattern=black is-live=true ! "
|
||||||
f"videorate ! " # <--- TIMING ENFORCER
|
f"videorate ! "
|
||||||
f"video/x-raw,width={INTERNAL_WIDTH},height={INTERNAL_HEIGHT},format=I420,framerate=60/1 ! "
|
f"video/x-raw,width={INTERNAL_WIDTH},height={INTERNAL_HEIGHT},format=I420,framerate=60/1 ! "
|
||||||
f"textoverlay text=\"DISCONNECTED\" valignment=center halignment=center font-desc=\"Sans, 48\" ! "
|
f"textoverlay text=\"DISCONNECTED\" valignment=center halignment=center font-desc=\"Sans, 48\" ! "
|
||||||
"nvvideoconvert compute-hw=1 ! "
|
"nvvideoconvert compute-hw=1 ! "
|
||||||
@ -181,28 +223,21 @@ class GStreamerPipeline(threading.Thread):
|
|||||||
sources_str += source
|
sources_str += source
|
||||||
|
|
||||||
# 3. MUXER & PROCESSING
|
# 3. MUXER & PROCESSING
|
||||||
# FIX 2: batched-push-timeout=33000
|
|
||||||
# This tells the muxer: "If you have data, send it every 33ms (30fps). Don't wait forever."
|
|
||||||
|
|
||||||
# FIX 3: Output Videorate
|
|
||||||
# We process internally at 60fps (best for camera driver), but we DROP to 30fps
|
|
||||||
# for the web stream. This makes the network stream buttery smooth and consistent.
|
|
||||||
|
|
||||||
processing = (
|
processing = (
|
||||||
f"nvstreammux name=m batch-size={TARGET_NUM_CAMS} width={INTERNAL_WIDTH} height={INTERNAL_HEIGHT} "
|
f"nvstreammux name=m batch-size={TARGET_NUM_CAMS} width={INTERNAL_WIDTH} height={INTERNAL_HEIGHT} "
|
||||||
f"live-source=1 batched-push-timeout=33000 ! " # <--- TIMEOUT FIX
|
f"live-source=1 batched-push-timeout=33000 ! "
|
||||||
f"nvmultistreamtiler width={WEB_WIDTH} height={WEB_HEIGHT} rows=1 columns={TARGET_NUM_CAMS} ! "
|
f"nvmultistreamtiler width={WEB_WIDTH} height={WEB_HEIGHT} rows=1 columns={TARGET_NUM_CAMS} ! "
|
||||||
"nvvideoconvert compute-hw=1 ! "
|
"nvvideoconvert compute-hw=1 ! "
|
||||||
"video/x-raw(memory:NVMM) ! "
|
"video/x-raw(memory:NVMM) ! "
|
||||||
"videorate drop-only=true ! " # <--- DROPPING FRAMES CLEANLY
|
"videorate drop-only=true ! "
|
||||||
"video/x-raw(memory:NVMM), framerate=30/1 ! " # <--- Force 30 FPS Output
|
"video/x-raw(memory:NVMM), framerate=30/1 ! "
|
||||||
f"nvjpegenc quality=60 ! "
|
f"nvjpegenc quality=60 ! "
|
||||||
"appsink name=sink emit-signals=True sync=False max-buffers=1 drop=True"
|
"appsink name=sink emit-signals=True sync=False max-buffers=1 drop=True"
|
||||||
)
|
)
|
||||||
|
|
||||||
pipeline_str = f"{sources_str} {processing}"
|
pipeline_str = f"{sources_str} {processing}"
|
||||||
|
|
||||||
print(f"Launching SMOOTH Pipeline...")
|
print(f"Launching Optimized Pipeline (All Cams Forced to 20ms Shutter)...")
|
||||||
self.pipeline = Gst.parse_launch(pipeline_str)
|
self.pipeline = Gst.parse_launch(pipeline_str)
|
||||||
appsink = self.pipeline.get_by_name("sink")
|
appsink = self.pipeline.get_by_name("sink")
|
||||||
appsink.connect("new-sample", self.on_new_sample)
|
appsink.connect("new-sample", self.on_new_sample)
|
||||||
@ -225,7 +260,7 @@ def index():
|
|||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Basler 3-Cam (Smooth)</h1>
|
<h1>Basler Final Feed</h1>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div class="hud" id="fps-counter">FPS: --</div>
|
<div class="hud" id="fps-counter">FPS: --</div>
|
||||||
<img src="{{ url_for('video_feed') }}">
|
<img src="{{ url_for('video_feed') }}">
|
||||||
@ -249,8 +284,7 @@ def video_feed():
|
|||||||
with buffer_lock:
|
with buffer_lock:
|
||||||
if frame_buffer:
|
if frame_buffer:
|
||||||
yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame_buffer + b'\r\n')
|
yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame_buffer + b'\r\n')
|
||||||
# Sleep 33ms (30 FPS)
|
time.sleep(0.016)
|
||||||
time.sleep(0.033)
|
|
||||||
count += 1
|
count += 1
|
||||||
if count % 200 == 0: gc.collect()
|
if count % 200 == 0: gc.collect()
|
||||||
return Response(generate(), mimetype='multipart/x-mixed-replace; boundary=frame')
|
return Response(generate(), mimetype='multipart/x-mixed-replace; boundary=frame')
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user