-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add full digicam example. * Update digicam config. * Clean up digicam example. * Setting relative path. * Set image resolution remotely. * Add checks that are in on-device script. * Add options for background image. * Update CHANGELOG.
- Loading branch information
Showing
13 changed files
with
203 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# python scripts/measure/digicam_example.py | ||
hydra: | ||
job: | ||
chdir: True # change to output folder | ||
|
||
rpi: | ||
username: null | ||
hostname: null | ||
|
||
# mask parameters | ||
mask: | ||
fp: null # provide path, otherwise generate with seed | ||
seed: 1 | ||
shape: [54, 26] | ||
center: [57, 77] | ||
|
||
# measurement parameters | ||
capture: | ||
fp: null | ||
exp: 0.5 | ||
sensor: rpi_hq | ||
script: ~/LenslessPiCam/scripts/measure/on_device_capture.py | ||
iso: 100 | ||
config_pause: 1 | ||
sensor_mode: "0" | ||
nbits_out: 8 | ||
nbits_capture: 12 | ||
legacy: True | ||
gray: False | ||
fn: raw_data | ||
bayer: True | ||
awb_gains: [1.6, 1.2] | ||
rgb: True | ||
down: 8 | ||
flip: True | ||
|
||
# reconstruction parameters | ||
recon: | ||
torch_device: 'cpu' | ||
n_iter: 100 # number of iterations of ADMM |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
""" | ||
DigiCam example to remotely: | ||
1. Set mask pattern. | ||
2. Capture image. | ||
3. Reconstruct image with simulated PSF. | ||
TODO: display image. At the moment should be done with `scripts/measure/remote_display.py` | ||
""" | ||
|
||
|
||
import hydra | ||
from hydra.utils import to_absolute_path | ||
import numpy as np | ||
from lensless.hardware.slm import set_programmable_mask, adafruit_sub2full | ||
from lensless.hardware.utils import capture | ||
import torch | ||
from lensless import ADMM | ||
from lensless.utils.io import save_image | ||
from lensless.hardware.trainable_mask import AdafruitLCD | ||
from lensless.utils.io import load_image | ||
|
||
|
||
@hydra.main(version_base=None, config_path="../../configs", config_name="digicam_example") | ||
def digicam(config): | ||
measurement_fp = config.capture.fp | ||
mask_fp = config.mask.fp | ||
seed = config.mask.seed | ||
rpi_username = config.rpi.username | ||
rpi_hostname = config.rpi.hostname | ||
mask_shape = config.mask.shape | ||
mask_center = config.mask.center | ||
torch_device = config.recon.torch_device | ||
capture_config = config.capture | ||
|
||
# load mask | ||
if mask_fp is not None: | ||
mask_vals = np.load(to_absolute_path(mask_fp)) | ||
else: | ||
# create random mask within [0, 1] | ||
np.random.seed(seed) | ||
mask_vals = np.random.uniform(0, 1, mask_shape) | ||
|
||
# simulate PSF | ||
mask = AdafruitLCD( | ||
initial_vals=torch.from_numpy(mask_vals.astype(np.float32)), | ||
sensor=capture_config["sensor"], | ||
slm="adafruit", | ||
downsample=capture_config["down"], | ||
flipud=capture_config["flip"], | ||
# color_filter=color_filter, | ||
) | ||
psf = mask.get_psf().to(torch_device).detach() | ||
psf_fp = "digicam_psf.png" | ||
save_image(psf[0].cpu().numpy(), psf_fp) | ||
print(f"PSF shape: {psf.shape}") | ||
print(f"PSF saved to {psf_fp}") | ||
|
||
if measurement_fp is not None: | ||
# load image | ||
img = load_image( | ||
to_absolute_path(measurement_fp), | ||
verbose=True, | ||
) | ||
|
||
else: | ||
## measure data | ||
# -- prepare full mask | ||
pattern = adafruit_sub2full( | ||
mask_vals, | ||
center=mask_center, | ||
) | ||
|
||
# -- set mask | ||
print("Setting mask") | ||
set_programmable_mask( | ||
pattern, | ||
"adafruit", | ||
rpi_username=rpi_username, | ||
rpi_hostname=rpi_hostname, | ||
) | ||
|
||
# -- capture | ||
print("Capturing") | ||
localfile, img = capture( | ||
rpi_username=rpi_username, | ||
rpi_hostname=rpi_hostname, | ||
verbose=False, | ||
**capture_config, | ||
) | ||
print(f"Captured to {localfile}") | ||
|
||
""" analyze image """ | ||
print("image range: ", img.min(), img.max()) | ||
|
||
""" reconstruction """ | ||
# -- normalize | ||
img = img.astype(np.float32) / img.max() | ||
# prep | ||
img = torch.from_numpy(img) | ||
# -- if [H, W, C] -> [D, H, W, C] | ||
if len(img.shape) == 3: | ||
img = img.unsqueeze(0) | ||
if capture_config["flip"]: | ||
img = torch.rot90(img, dims=(-3, -2), k=2) | ||
|
||
# reconstruct | ||
print("Reconstructing") | ||
recon = ADMM(psf) | ||
recon.set_data(img.to(psf.device)) | ||
res = recon.apply(disp_iter=None, plot=False, n_iter=config.recon.n_iter) | ||
res_np = res[0].cpu().numpy() | ||
res_np = res_np / res_np.max() | ||
lensless_np = img[0].cpu().numpy() | ||
save_image(lensless_np, "digicam_raw.png") | ||
save_image(res_np, "digicam_recon.png") | ||
|
||
print("Done") | ||
|
||
|
||
if __name__ == "__main__": | ||
digicam() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters