Skip to content

Commit ecce483

Browse files
committed
add member NO & staticmethod auto to Preview enum
Signed-off-by: Asadullah Shaikh <asadshaikh20022002@gmail.com>
1 parent be46a5d commit ecce483

14 files changed

+55
-57
lines changed

examples/capture_old_request.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
import time
77

8-
from picamera2 import Picamera2
8+
from picamera2 import Picamera2, Preview
99

1010
picam2 = Picamera2()
1111
capture_config = picam2.create_still_configuration()
12-
picam2.start(show_preview=True)
12+
picam2.start(show_preview=Preview.auto())
1313

1414
time.sleep(1)
1515

examples/imx500/imx500_classification_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import cv2
77
import numpy as np
88

9-
from picamera2 import CompletedRequest, MappedArray, Picamera2
9+
from picamera2 import CompletedRequest, MappedArray, Picamera2, Preview
1010
from picamera2.devices import IMX500
1111
from picamera2.devices.imx500 import NetworkIntrinsics
1212
from picamera2.devices.imx500.postprocess import softmax
@@ -146,7 +146,7 @@ def get_args():
146146
config = picam2.create_preview_configuration(controls={"FrameRate": intrinsics.inference_rate}, buffer_count=12)
147147

148148
imx500.show_network_fw_progress_bar()
149-
picam2.start(config, show_preview=True)
149+
picam2.start(config, show_preview=Preview.auto())
150150
if intrinsics.preserve_aspect_ratio:
151151
imx500.set_auto_aspect_ratio()
152152
# Register the callback to parse and draw classification results

examples/imx500/imx500_object_detection_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import cv2
66
import numpy as np
77

8-
from picamera2 import MappedArray, Picamera2
8+
from picamera2 import MappedArray, Picamera2, Preview
99
from picamera2.devices import IMX500
1010
from picamera2.devices.imx500 import (NetworkIntrinsics,
1111
postprocess_nanodet_detection)
@@ -168,7 +168,7 @@ def get_args():
168168
config = picam2.create_preview_configuration(controls={"FrameRate": intrinsics.inference_rate}, buffer_count=12)
169169

170170
imx500.show_network_fw_progress_bar()
171-
picam2.start(config, show_preview=True)
171+
picam2.start(config, show_preview=Preview.auto())
172172

173173
if intrinsics.preserve_aspect_ratio:
174174
imx500.set_auto_aspect_ratio()

examples/imx500/imx500_pose_estimation_higherhrnet_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import numpy as np
66

7-
from picamera2 import CompletedRequest, MappedArray, Picamera2
7+
from picamera2 import CompletedRequest, MappedArray, Picamera2, Preview
88
from picamera2.devices.imx500 import IMX500, NetworkIntrinsics
99
from picamera2.devices.imx500.postprocess import COCODrawer
1010
from picamera2.devices.imx500.postprocess_highernet import \
@@ -109,7 +109,7 @@ def get_drawer():
109109
config = picam2.create_preview_configuration(controls={'FrameRate': intrinsics.inference_rate}, buffer_count=12)
110110

111111
imx500.show_network_fw_progress_bar()
112-
picam2.start(config, show_preview=True)
112+
picam2.start(config, show_preview=Preview.auto())
113113
imx500.set_auto_aspect_ratio()
114114
picam2.pre_callback = picamera2_pre_callback
115115

examples/imx500/imx500_segmentation_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import numpy as np
77

8-
from picamera2 import CompletedRequest, Picamera2
8+
from picamera2 import CompletedRequest, Picamera2, Preview
99
from picamera2.devices import IMX500
1010
from picamera2.devices.imx500 import NetworkIntrinsics
1111

@@ -94,7 +94,7 @@ def get_args():
9494
picam2 = Picamera2(imx500.camera_num)
9595
config = picam2.create_preview_configuration(controls={'FrameRate': intrinsics.inference_rate}, buffer_count=12)
9696
imx500.show_network_fw_progress_bar()
97-
picam2.start(config, show_preview=True)
97+
picam2.start(config, show_preview=Preview.auto())
9898
picam2.pre_callback = create_and_draw_masks
9999

100100
while True:

examples/stereo_preview.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import time
44
from threading import Lock
55

6-
from picamera2 import MappedArray, Picamera2, libcamera
6+
from picamera2 import MappedArray, Picamera2, Preview, libcamera
77

88
cam2_request = None
99
lock = Lock()
@@ -57,7 +57,7 @@ def save_request(request):
5757
controls={"ScalerCrop": (0, 0, picam2a.sensor_resolution[0], picam2a.sensor_resolution[1])}
5858
)
5959
picam2a.configure(main_config)
60-
picam2a.start_preview(True)
60+
picam2a.start_preview(Preview.auto())
6161

6262
# Configure as half frame normally
6363
picam2b = Picamera2(1)

examples/still_capture_with_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import time
66

7-
from picamera2 import Picamera2
7+
from picamera2 import Picamera2, Preview
88

99
picam2 = Picamera2()
1010

@@ -15,7 +15,7 @@
1515
picam2.still_configuration.enable_raw()
1616
picam2.still_configuration.raw.size = picam2.sensor_resolution
1717

18-
picam2.start("preview", show_preview=True)
18+
picam2.start("preview", show_preview=Preview.auto())
1919
time.sleep(2)
2020

2121
picam2.switch_mode_and_capture_file("still", "test_full.jpg")

examples/title_bar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import time
44

5-
from picamera2 import Picamera2
5+
from picamera2 import Picamera2, Preview
66

77
picam2 = Picamera2()
8-
picam2.start(show_preview=True)
8+
picam2.start(show_preview=Preview.auto())
99
time.sleep(0.5)
1010

1111
# Or you could do this before starting the camera.

picamera2/picamera2.py

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ class Preview(Enum):
5353
DRM = DrmPreview
5454
QT = QtPreview
5555
QTGL = QtGlPreview
56+
NO = None
57+
58+
@staticmethod
59+
def auto():
60+
# Crude attempt at "autodetection" but which will mostly (?) work. We will
61+
# probably find situations that need fixing, VNC perhaps.
62+
display = os.getenv('DISPLAY')
63+
if display is None:
64+
return Preview.DRM
65+
elif display.startswith(':'):
66+
return Preview.QTGL
67+
else:
68+
return Preview.QT
5669

5770

5871
class GlobalCameraInfo(TypedDict):
@@ -578,34 +591,23 @@ def start_preview(self, preview=None, **kwargs) -> None:
578591
"""
579592
Start the given preview which drives the camera processing.
580593
581-
The preview may be either:
582-
None or False - in which case a NullPreview is made,
583-
True - which we hope in future to use to autodetect
584-
a Preview enum value - in which case a preview of that type is made,
585-
or an actual preview object.
594+
preview - a Preview enum or an actual preview object.
595+
Pass Preview.auto() to autodetect. Defaults to NullPreview.
586596
587-
When using the enum form, extra keyword arguments can be supplied that
588-
will be forwarded to the preview class constructor.
597+
**kwargs - keyword arguments to be forwarded to the preview
598+
class constructor, when using the enum form
589599
"""
590-
if self._event_loop_running:
591-
raise RuntimeError("An event loop is already running")
592-
593-
if preview is True:
594-
# Crude attempt at "autodetection" but which will mostly (?) work. We will
595-
# probably find situations that need fixing, VNC perhaps.
596-
display = os.getenv('DISPLAY')
597-
if display is None:
598-
preview = Preview.DRM.value(**kwargs)
599-
elif display.startswith(':'):
600-
preview = Preview.QTGL.value(**kwargs)
601-
else:
602-
preview = Preview.QT.value(**kwargs)
603-
elif preview is False or preview is None:
604-
preview = Preview.NULL.value(**kwargs)
600+
if preview is None:
601+
preview = NullPreview(**kwargs)
605602
elif isinstance(preview, Preview):
603+
if preview is Preview.NO:
604+
return
606605
preview = preview.value(**kwargs)
607606
# Assume it's already a preview object.
608607

608+
if self._event_loop_running:
609+
raise RuntimeError("An event loop is already running")
610+
609611
# The preview windows call the attach_preview method.
610612
self._preview_stopped.clear()
611613
preview.start(self)
@@ -1189,20 +1191,16 @@ def start(self, config=None, show_preview=None) -> None:
11891191
11901192
Camera controls may be sent to the camera before it starts running.
11911193
1192-
config - if given it is used to configure the camera.
1193-
Defaults to 'preview' configuration if the camera is unconfigured.
1194+
config - Camera configuration to be set. Defaults to 'preview' configuration.
1195+
Note: if the camera is already configured, this has no effect.
11941196
1195-
show_preview - whether to show a preview window. By default no visible preview window
1196-
will be shown but the NullPreview would still run. True will attempt to autodetect.
1197-
False would skip starting an event loop altogether. A Preview enum could also given.
1197+
show_preview - a Preview enum or an actual preview object.
1198+
Pass Preview.auto() to autodetect. Defaults to NullPreview.
11981199
Note: if an event loop is already running, this has no effect.
11991200
"""
1200-
if not self.camera_config and config is None:
1201-
config = "preview"
1202-
if config is not None:
1201+
if not self.camera_config:
12031202
self.configure(config)
1204-
# By default we will create an event loop if there isn't one running already.
1205-
if show_preview is not False and not self._event_loop_running:
1203+
if not self._event_loop_running:
12061204
self.start_preview(show_preview)
12071205
self.start_()
12081206

tests/crop_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import cv2
66

7-
from picamera2 import Picamera2, Platform
7+
from picamera2 import Picamera2, Platform, Preview
88

99
# VC4 platforms do not support different crops for the two outputs.
1010
if Picamera2.platform == Platform.VC4:
@@ -18,7 +18,7 @@
1818
lores={"size": (320, 320), "format": 'XRGB8888', "preserve_ar": l},
1919
display="main")
2020
picam2.configure(cfg)
21-
picam2.start(show_preview=True)
21+
picam2.start(show_preview=Preview.auto())
2222

2323
for _ in range(50):
2424
im = picam2.capture_array("lores")

0 commit comments

Comments
 (0)