Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

[WIP] Remove lxml dep from local file and handle relative path - nodes PR:302 #402

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions docs/nodes/IO/IMAGING/OPEN_WEBCAM/OPEN_WEBCAM.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

[//]: # (Custom component imports)

import DocString from '@site/src/components/DocString';
import PythonCode from '@site/src/components/PythonCode';
import AppDisplay from '@site/src/components/AppDisplay';
import SectionBreak from '@site/src/components/SectionBreak';
import AppendixSection from '@site/src/components/AppendixSection';

[//]: # (Docstring)

import DocstringSource from '!!raw-loader!./a1-[autogen]/docstring.txt';
import PythonSource from '!!raw-loader!./a1-[autogen]/python_code.txt';

<DocString>{DocstringSource}</DocString>
<PythonCode GLink='IO/IMAGING/OPEN_WEBCAM/OPEN_WEBCAM.py'>{PythonSource}</PythonCode>

<SectionBreak />



[//]: # (Examples)

## Examples

import Example1 from './examples/EX1/example.md';
import App1 from '!!raw-loader!./examples/EX1/app.json';



<AppDisplay
nodeLabel='OPEN_WEBCAM'
appImg={''}
outputImg={''}
>
{App1}
</AppDisplay>

<Example1 />

<SectionBreak />



[//]: # (Appendix)

import Notes from './appendix/notes.md';
import Hardware from './appendix/hardware.md';
import Media from './appendix/media.md';

## Appendix

<AppendixSection index={0} folderPath='nodes/IO/IMAGING/OPEN_WEBCAM/appendix/'><Notes /></AppendixSection>
<AppendixSection index={1} folderPath='nodes/IO/IMAGING/OPEN_WEBCAM/appendix/'><Hardware /></AppendixSection>
<AppendixSection index={2} folderPath='nodes/IO/IMAGING/OPEN_WEBCAM/appendix/'><Media /></AppendixSection>


12 changes: 12 additions & 0 deletions docs/nodes/IO/IMAGING/OPEN_WEBCAM/a1-[autogen]/docstring.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
The OPEN_WEBCAM node opens a connection with the selected camera.

Parameters
----------
camera : Camera
The camera to use.
resolution : select
Camera resolution. Choose from a few options.

Returns
-------
None
20 changes: 20 additions & 0 deletions docs/nodes/IO/IMAGING/OPEN_WEBCAM/a1-[autogen]/python_code.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import cv2
from flojoy import CameraDevice, flojoy, DataContainer
from flojoy.connection_manager import DeviceConnectionManager
from typing import Optional, Literal


@flojoy(deps={"opencv-python-headless": "4.7.0.72"})
def OPEN_WEBCAM(
camera: CameraDevice,
default: Optional[DataContainer] = None,
) -> Optional[DataContainer]:


if not camera:
raise ValueError("No camera selected")

cam = cv2.VideoCapture(camera.get_id())
DeviceConnectionManager.register_connection(camera, cam)

return None
1 change: 1 addition & 0 deletions docs/nodes/IO/IMAGING/OPEN_WEBCAM/appendix/hardware.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This node does not require any peripheral hardware to operate. Please see INSTRUMENTS for nodes that interact with the physical world through connected hardware.
1 change: 1 addition & 0 deletions docs/nodes/IO/IMAGING/OPEN_WEBCAM/appendix/media.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
No supporting screenshots, photos, or videos have been added to the media.md file for this node.
1 change: 1 addition & 0 deletions docs/nodes/IO/IMAGING/OPEN_WEBCAM/appendix/notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
No theory or technical notes have been contributed for this node yet.
204 changes: 204 additions & 0 deletions docs/nodes/IO/IMAGING/OPEN_WEBCAM/examples/EX1/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
{
"rfInstance": {
"nodes": [
{
"width": 192,
"height": 192,
"id": "OPEN_WEBCAM-89c7016f-6198-4bee-bb37-0ffa21729da5",
"type": "IO",
"data": {
"id": "OPEN_WEBCAM-89c7016f-6198-4bee-bb37-0ffa21729da5",
"label": "OPEN WEBCAM",
"func": "OPEN_WEBCAM",
"type": "IO",
"ctrls": {
"camera": {
"type": "CameraDevice",
"default": null,
"desc": null,
"overload": null,
"functionName": "OPEN_WEBCAM",
"param": "camera",
"value": ""
}
},
"initCtrls": {},
"inputs": [
{
"name": "default",
"id": "default",
"type": "Any",
"multiple": false,
"desc": null
}
],
"outputs": [
{
"name": "default",
"id": "default",
"type": "Any",
"desc": null
}
],
"pip_dependencies": [
{
"name": "opencv-python-headless",
"v": "4.7.0.72"
}
],
"path": "IO/IMAGING/OPEN_WEBCAM/OPEN_WEBCAM.py",
"selected": false
},
"position": {
"x": -179.01679378861343,
"y": 3.074962043597168
},
"selected": false,
"positionAbsolute": {
"x": -179.01679378861343,
"y": 3.074962043597168
},
"dragging": true
},
{
"width": 192,
"height": 192,
"id": "WEBCAM-89b91b3a-0682-44db-95d9-b1e26d1dd12c",
"type": "IO",
"data": {
"id": "WEBCAM-89b91b3a-0682-44db-95d9-b1e26d1dd12c",
"label": "WEBCAM",
"func": "WEBCAM",
"type": "IO",
"ctrls": {
"connection": {
"type": "CameraConnection",
"default": null,
"desc": null,
"overload": null,
"functionName": "WEBCAM",
"param": "connection",
"value": ""
},
"resolution": {
"type": "select",
"options": [
"default",
"640x360",
"640x480",
"1280x720",
"1920x1080"
],
"default": "default",
"desc": "Camera resolution. Choose from a few options.",
"overload": null,
"functionName": "WEBCAM",
"param": "resolution",
"value": "default"
}
},
"initCtrls": {},
"inputs": [
{
"name": "default",
"id": "default",
"type": "Any",
"multiple": false,
"desc": null
}
],
"outputs": [
{
"name": "default",
"id": "default",
"type": "Image",
"desc": null
}
],
"pip_dependencies": [
{
"name": "opencv-python-headless",
"v": "4.7.0.72"
}
],
"path": "IO/IMAGING/WEBCAM/WEBCAM.py",
"selected": false
},
"position": {
"x": 179.0676354144525,
"y": 0.6447218141543658
},
"selected": false,
"positionAbsolute": {
"x": 179.0676354144525,
"y": 0.6447218141543658
},
"dragging": true
},
{
"width": 225,
"height": 226,
"id": "IMAGE-d27c4496-301f-4882-8670-6bb84ebe7e06",
"type": "VISUALIZERS",
"data": {
"id": "IMAGE-d27c4496-301f-4882-8670-6bb84ebe7e06",
"label": "IMAGE",
"func": "IMAGE",
"type": "VISUALIZERS",
"ctrls": {},
"initCtrls": {},
"inputs": [
{
"name": "default",
"id": "default",
"type": "Image|Grayscale",
"multiple": false,
"desc": "the DataContainer to be visualized"
}
],
"outputs": [
{
"name": "default",
"id": "default",
"type": "Plotly",
"desc": "the DataContainer containing the Plotly Image visualization of the input image"
}
],
"path": "VISUALIZERS/PLOTLY/IMAGE/IMAGE.py",
"selected": false
},
"position": {
"x": 515.9438185125092,
"y": -20.240488117947223
},
"selected": false,
"positionAbsolute": {
"x": 515.9438185125092,
"y": -20.240488117947223
},
"dragging": true
}
],
"edges": [
{
"source": "WEBCAM-89b91b3a-0682-44db-95d9-b1e26d1dd12c",
"sourceHandle": "default",
"target": "IMAGE-d27c4496-301f-4882-8670-6bb84ebe7e06",
"targetHandle": "default",
"id": "reactflow__edge-WEBCAM-89b91b3a-0682-44db-95d9-b1e26d1dd12cdefault-IMAGE-d27c4496-301f-4882-8670-6bb84ebe7e06default"
},
{
"source": "OPEN_WEBCAM-89c7016f-6198-4bee-bb37-0ffa21729da5",
"sourceHandle": "default",
"target": "WEBCAM-89b91b3a-0682-44db-95d9-b1e26d1dd12c",
"targetHandle": "default",
"id": "reactflow__edge-OPEN_WEBCAM-89c7016f-6198-4bee-bb37-0ffa21729da5default-WEBCAM-89b91b3a-0682-44db-95d9-b1e26d1dd12cdefault"
}
],
"viewport": {
"x": 255.10425070720453,
"y": 317.88940714410813,
"zoom": 0.5
}
}
}
12 changes: 12 additions & 0 deletions docs/nodes/IO/IMAGING/OPEN_WEBCAM/examples/EX1/example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: OPEN_WEBCAM
description: In this example, the OPEN_WEBCAM opens the connection with the connected camera, then uses the `WEBCAM` node takes and returns a picture. The IMSHOW node then displays the image taken by the camera.
keyword: [Python, Instrument, Web cam, Camera, Python webcam integration, Camera instrument in Python, Capture images and videos, Streamline webcam usage, Python-based camera control, Webcam integration techniques, Python image and video capture, Enhance projects with webcam, Accurate media processing, Webcam usage with Python]
image: https://raw.githubusercontent.com/flojoy-ai/docs/main/docs/nodes/INSTRUMENTS/WEB_CAM/CAMERA/examples/EX1/output.jpeg
---

In this example app, the [`CAMERA`](https://github.com/flojoy-io/nodes/blob/main/INSTRUMENTS/WEB_CAM/CAMERA/CAMERA.py) node takes and returns a picture from a camera connected to the computer.

The camera first has to be opened with the [`OPEN_CAMERA`](https://github.com/flojoy-io/nodes/blob/main/INSTRUMENTS/WEB_CAM/CAMERA/CAMERA.py) node, which requires you to select which camera to use.

The [`IMSHOW`](https://github.com/flojoy-io/nodes/blob/main/VISUALIZERS/PLOTLY/TABLE/TABLE.py) node displays the image taken by the camera that was selected.
6 changes: 3 additions & 3 deletions docs/nodes/IO/IMAGING/WEBCAM/a1-[autogen]/docstring.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
The CAMERA node acquires an image using the selected camera.

If no camera is detected, an error would be shown.
The selected camera must be opened already using the OPEN_WEBCAM node.

Parameters
----------
camera_ind : int
Camera index (i.e. camera identifier).
connection : Camera
The opened camera connection to use.
resolution : select
Camera resolution. Choose from a few options.

Expand Down
28 changes: 12 additions & 16 deletions docs/nodes/IO/IMAGING/WEBCAM/a1-[autogen]/python_code.txt
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
import cv2
import os
from flojoy import flojoy, DataContainer
from flojoy import flojoy, DataContainer, CameraConnection, Image
from typing import Optional, Literal
from PIL import Image
import numpy as np


@flojoy(deps={"opencv-python-headless": "4.7.0.72"})
@flojoy(deps={"opencv-python-headless": "4.7.0.72"}, inject_connection=True)
def WEBCAM(
connection: CameraConnection,
default: Optional[DataContainer] = None,
camera_ind: int = -1,
resolution: Literal[
"default", "640x360", "640x480", "1280x720", "1920x1080"
] = "default",
) -> DataContainer:
) -> Image:

cam = connection.get_handle()

try:
camera = cv2.VideoCapture(camera_ind)
if resolution != "default":
resolution = resolution.split("x")
try:
camera.set(cv2.CAP_PROP_FRAME_WIDTH, int(resolution[0]))
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, int(resolution[1]))
cam.set(cv2.CAP_PROP_FRAME_WIDTH, int(resolution[0]))
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, int(resolution[1]))
except cv2.error as camera_error:
print(f"Invalid resolution ({resolution}). Try a lower value.")
raise camera_error

if not camera.isOpened():
if not cam.isOpened():
raise cv2.error("Failed to open camera")

result, BGR_img = camera.read()
result, BGR_img = cam.read()

if not result:
raise cv2.error("Failed to capture image")
camera.release()
del camera
# cam.release()
# del cam

RGB_img = cv2.cvtColor(BGR_img, cv2.COLOR_BGR2RGB)

Expand All @@ -49,8 +46,7 @@ def WEBCAM(
else:
alpha_channel = None

camera_image = DataContainer(
type="Image",
camera_image = Image(
r=red_channel,
g=green_channel,
b=blue_channel,
Expand Down
Loading