Skip to content

Commit 2e5378f

Browse files
committed
deprecate show_preview in favor of preview with the following valid values:
- None (default; mimics the default behavior of `show_preview`, change this to `NullPreview` once deprecation is complete) - `Preview` enum member - a preview object specifically, passing `bool` to `preview` has been deprecated in favor of `Preview.auto()` & `Preview.NO` Signed-off-by: Asadullah Shaikh <asadshaikh20022002@gmail.com>
1 parent a96bbce commit 2e5378f

15 files changed

+87
-86
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(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, 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, preview=Preview.auto())
172172

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

examples/imx500/imx500_object_detection_demo_mp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def get_args():
173173
config = picam2.create_preview_configuration(main, controls={"FrameRate": intrinsics.inference_rate}, buffer_count=12)
174174

175175
imx500.show_network_fw_progress_bar()
176-
picam2.start(config, show_preview=False)
176+
picam2.start(config)
177177
if intrinsics.preserve_aspect_ratio:
178178
imx500.set_auto_aspect_ratio()
179179

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, 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, 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", 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(preview=Preview.auto())
99
time.sleep(0.5)
1010

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

picamera2/picamera2.py

Lines changed: 61 additions & 60 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,26 @@ 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.
586-
587-
When using the enum form, extra keyword arguments can be supplied that
588-
will be forwarded to the preview class constructor.
594+
preview - a Preview enum or an actual preview object.
595+
Pass Preview.auto() to autodetect. Defaults to NullPreview.
596+
Additional keyword arguments may be supplied which will be
597+
forwarded to the preview class constructor.
589598
"""
590-
if self._event_loop_running:
591-
raise RuntimeError("An event loop is already running")
599+
if isinstance(preview, bool):
600+
_log.error("Passing bool to preview parameter is deprecated")
601+
preview = Preview.auto() if preview else None
592602

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)
603+
if preview is None:
604+
preview = NullPreview(**kwargs)
605605
elif isinstance(preview, Preview):
606+
if preview is Preview.NO:
607+
return
606608
preview = preview.value(**kwargs)
607609
# Assume it's already a preview object.
608610

611+
if self._event_loop_running:
612+
raise RuntimeError("An event loop is already running")
613+
609614
# The preview windows call the attach_preview method.
610615
self._preview_stopped.clear()
611616
preview.start(self)
@@ -1183,32 +1188,27 @@ def start_(self):
11831188
_log.info("Camera started")
11841189
self.started = True
11851190

1186-
def start(self, config=None, show_preview=False) -> None:
1191+
def start(self, config=None, show_preview=False, preview=None) -> None:
11871192
"""
11881193
Start the camera system running.
11891194
11901195
Camera controls may be sent to the camera before it starts running.
11911196
1192-
The following parameters may be supplied:
1197+
config - Camera configuration to be set. Defaults to 'preview' configuration.
1198+
Note: if the camera is already configured, this has no effect.
11931199
1194-
config - if not None this is used to configure the camera. This is just a
1195-
convenience so that you don't have to call configure explicitly.
1196-
1197-
show_preview - whether to show a preview window. You can pass in the preview
1198-
type or True to attempt to autodetect. If left as False you'll get no
1199-
visible preview window but the "NULL preview" will still be run. The
1200-
value None would mean no event loop runs at all and you would have to
1201-
implement your own.
1200+
preview - A Preview enum or an actual preview object.
1201+
Pass Preview.auto() to autodetect. Defaults to NullPreview.
1202+
Note: if an event loop is already running, this parameter has no effect.
12021203
"""
1203-
if not self.camera_config and config is None:
1204-
config = "preview"
1205-
if config is not None:
1206-
self.configure(config)
1204+
if preview is None and show_preview is not False:
1205+
_log.error("show_preview is deprecated, use preview instead")
1206+
preview = Preview.NO if show_preview is None else show_preview
1207+
12071208
if not self.camera_config:
1208-
raise RuntimeError("Camera has not been configured")
1209-
# By default we will create an event loop if there isn't one running already.
1210-
if show_preview is not None and not self._event_loop_running:
1211-
self.start_preview(show_preview)
1209+
self.configure(config)
1210+
if not self._event_loop_running:
1211+
self.start_preview(preview)
12121212
self.start_()
12131213

12141214
def cancel_all_and_flush(self) -> None:
@@ -2394,7 +2394,7 @@ def set_overlay(self, overlay) -> None:
23942394
def start_and_capture_files(self, name="image{:03d}.jpg",
23952395
initial_delay=1, preview_mode="preview",
23962396
capture_mode="still", num_files=1, delay=1,
2397-
show_preview=True, exif_data=None) -> None:
2397+
show_preview=True, exif_data=None, preview=None) -> None:
23982398
"""This function makes capturing multiple images more convenient.
23992399
24002400
Should only be used in command line line applications (not from a Qt application, for example).
@@ -2417,34 +2417,35 @@ def start_and_capture_files(self, name="image{:03d}.jpg",
24172417
24182418
delay - the time delay for every capture after the first (default 1s).
24192419
2420-
show_preview - whether to show a preview window (default: yes). The preview window only
2421-
displays an image by default during the preview phase, so if captures are back-to-back
2422-
with delay zero, then there may be no images shown. This parameter only has any
2423-
effect if a preview is not already running. If it is, it would have to be stopped first
2424-
(with the stop_preview method).
2420+
preview - which preview to start (current-default: auto-detect; in-future: NullPreview).
2421+
The preview window only displays an image by default during the preview phase,
2422+
so if captures are back-to-back with delay zero, then there may be no images shown.
2423+
Note: if a preview is already running, this parameter has no effect.
24252424
24262425
exif_data - dictionary containing user defined exif data (based on `piexif`). This will
24272426
overwrite existing exif information generated by picamera2.
24282427
"""
2428+
_log.warning("FutureWarning: Parameter preview will default to NullPreview in a future release")
2429+
24292430
if self.started:
24302431
self.stop()
24312432
if delay:
24322433
# Show a preview between captures, so we will switch mode and back for each capture.
24332434
self.configure(preview_mode)
2434-
self.start(show_preview=show_preview)
2435+
self.start(show_preview=show_preview, preview=preview)
24352436
for i in range(num_files):
24362437
time.sleep(initial_delay if i == 0 else delay)
24372438
self.switch_mode_and_capture_file(capture_mode, name.format(i), exif_data=exif_data)
24382439
else:
24392440
# No preview between captures, it's more efficient just to stay in capture mode.
24402441
if initial_delay:
24412442
self.configure(preview_mode)
2442-
self.start(show_preview=show_preview)
2443+
self.start(show_preview=show_preview, preview=preview)
24432444
time.sleep(initial_delay)
24442445
self.switch_mode(capture_mode)
24452446
else:
24462447
self.configure(capture_mode)
2447-
self.start(show_preview=show_preview)
2448+
self.start(show_preview=show_preview, preview=preview)
24482449
for i in range(num_files):
24492450
self.capture_file(name.format(i), exif_data=exif_data)
24502451
if i == num_files - 1:
@@ -2453,7 +2454,7 @@ def start_and_capture_files(self, name="image{:03d}.jpg",
24532454
self.stop()
24542455

24552456
def start_and_capture_file(self, name="image.jpg", delay=1, preview_mode="preview",
2456-
capture_mode="still", show_preview=True, exif_data=None) -> None:
2457+
capture_mode="still", show_preview=True, exif_data=None, preview=None) -> None:
24572458
"""This function makes capturing a single image more convenient.
24582459
24592460
Should only be used in command line line applications (not from a Qt application, for example).
@@ -2471,21 +2472,22 @@ def start_and_capture_file(self, name="image.jpg", delay=1, preview_mode="previe
24712472
capture_mode - the camera mode to use to capture the still images (defaulting to the
24722473
Picamera2 object's still_configuration field).
24732474
2474-
show_preview - whether to show a preview window (default: yes). The preview window only
2475-
displays an image by default during the preview phase. This parameter only has any
2476-
effect if a preview is not already running. If it is, it would have to be stopped first
2477-
(with the stop_preview method).
2475+
preview - which preview to start (current-default: auto-detect; in-future: NullPreview).
2476+
The preview window only displays an image by default during the preview phase.
2477+
Note: if a preview is already running, this parameter has no effect.
24782478
24792479
exif_data - dictionary containing user defined exif data (based on `piexif`). This will
24802480
overwrite existing exif information generated by picamera2.
24812481
"""
2482+
_log.warning("FutureWarning: Parameter preview will default to NullPreview in a future release")
2483+
24822484
self.start_and_capture_files(name=name, initial_delay=delay, preview_mode=preview_mode,
24832485
capture_mode=capture_mode, num_files=1,
2484-
show_preview=show_preview,
2486+
show_preview=show_preview, preview=preview,
24852487
exif_data=exif_data)
24862488

24872489
def start_and_record_video(self, output, encoder=None, config=None, quality=Quality.MEDIUM,
2488-
show_preview=False, duration=0, audio=False) -> None:
2490+
show_preview=False, duration=0, audio=False, preview=None) -> None:
24892491
"""This function makes video recording more convenient.
24902492
24912493
Should only be used in command line applications (not from a Qt application, for example).
@@ -2507,9 +2509,8 @@ def start_and_record_video(self, output, encoder=None, config=None, quality=Qual
25072509
quality - an indication of the video quality to use. This will be ignored if the encoder
25082510
object was created with all its quality parameters (such as bitrate) filled in.
25092511
2510-
show_preview - whether to show a preview window (default: no). This parameter only has an
2511-
effect if a preview is not already running, in which case that preview would need
2512-
stopping first (using stop_preview) for any change to take effect.
2512+
preview - which preview to start (default: NullPreview).
2513+
Note: if a preview is already running, this parameter has no effect.
25132514
25142515
duration - the duration of the video. The function will wait this amount of time before
25152516
stopping the recording and returning. The default behaviour is to return immediately
@@ -2538,7 +2539,7 @@ def start_and_record_video(self, output, encoder=None, config=None, quality=Qual
25382539
if encoder is None:
25392540
encoder = H264Encoder()
25402541
self.start_encoder(encoder=encoder, output=output, quality=quality)
2541-
self.start(show_preview=show_preview)
2542+
self.start(show_preview=show_preview, preview=preview)
25422543
if duration:
25432544
time.sleep(duration)
25442545
self.stop_recording()

0 commit comments

Comments
 (0)