Skip to content
Open
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
192 changes: 192 additions & 0 deletions ai_services/vision/python/stream-video/stream-process-output.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "bacae64c",
"metadata": {},
"outputs": [],
"source": [
"from oci.object_storage import ObjectStorageClient\n",
"import time\n",
"import base64\n",
"from PIL import Image\n",
"from io import BytesIO\n",
"import cv2\n",
"import numpy as np\n",
"import json\n",
"import oci\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "019e28f5",
"metadata": {},
"outputs": [],
"source": [
"NAMESPACE = \"\"\n",
"BUCKET = \"\"\n",
"PREFIX = \"\"\n",
"CONFIG_PROFILE = \"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "20ecb251",
"metadata": {},
"outputs": [],
"source": [
"# Configure OCI client\n",
"config = oci.config.from_file('~/.oci/config', profile_name=CONFIG_PROFILE)\n",
"endpoint = \"https://objectstorage.us-ashburn-1.oraclecloud.com\"\n",
"token_file = config['security_token_file']\n",
"with open(token_file, 'r') as f:\n",
" token = f.read()\n",
"\n",
"private_key = oci.signer.load_private_key_from_file(config['key_file'])\n",
"signer = oci.auth.signers.SecurityTokenSigner(token, private_key)\n",
"object_storage_client = ObjectStorageClient(config=config, signer=signer, service_endpoint=endpoint)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ccf81481",
"metadata": {},
"outputs": [],
"source": [
"def decode_image(image_data):\n",
" \"\"\"Decode base64 image to OpenCV format and ensure it has 3 channels (RGB).\"\"\"\n",
" image_bytes = base64.b64decode(image_data)\n",
" image = Image.open(BytesIO(image_bytes))\n",
"\n",
" if image.mode not in (\"RGB\", \"RGBA\"):\n",
" image = image.convert(\"RGB\")\n",
"\n",
" return cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "81e8fb29",
"metadata": {},
"outputs": [],
"source": [
"def draw_faces(image, faces, frame):\n",
" \"\"\"Draw bounding boxes around detected faces.\"\"\"\n",
" height, width, _ = image.shape\n",
" for face in faces:\n",
" if face['name'] == 'weapon':\n",
" vertices = face['boundingPolygon']['normalizedVertices']\n",
" pts = [(int(v['x'] * width), int(v['y'] * height)) for v in vertices]\n",
" cv2.polylines(image, [np.array(pts)], isClosed=True, color=(0, 255, 0), thickness=2)\n",
" if frame is not None:\n",
" # Add frame number text in the top right corner\n",
" text = f\"Frame: {frame}\"\n",
" font = cv2.FONT_HERSHEY_SIMPLEX\n",
" font_scale = 1\n",
" color = (0, 255, 0)\n",
" thickness = 1\n",
" text_size = cv2.getTextSize(text, font, font_scale, thickness)[0]\n",
" text_x = width - text_size[0] - 10\n",
" text_y = 30\n",
" cv2.putText(image, text, (text_x, text_y), font, font_scale, color, thickness)\n",
"\n",
" return image"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "299aa92f",
"metadata": {},
"outputs": [],
"source": [
"def process_and_display(message):\n",
" \"\"\"Process detection data and update the window.\"\"\"\n",
" message = message.replace(\"'\", '\"') # Make it valid JSON\n",
" message_dict = json.loads(message)\n",
"\n",
" image = decode_image(message_dict['imageData'])\n",
" if len(message_dict['detectedObjects']) > 0:\n",
" face_id = message_dict['detectedObjects'][0]['objectId']\n",
" print(\"face detected\", face_id)\n",
"\n",
" print(message_dict['detectedObjects'])\n",
" # Uncomment if you want to visualize\n",
" annotated_image = draw_faces(image, message_dict['detectedObjects'], message_dict[\"imageData\"])\n",
" cv2.imshow(\"Face Detection\", annotated_image)\n",
" cv2.waitKey(1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1fafd752",
"metadata": {},
"outputs": [],
"source": [
"def main():\n",
" while True:\n",
" ts_ns = int(time.time() * 1_000_000_000) - 3_000_000_000\n",
" filename = f\"new1/frame_{ts_ns}.json\"\n",
"\n",
" objects = object_storage_client.list_objects(\n",
" namespace_name=NAMESPACE,\n",
" bucket_name=BUCKET,\n",
" prefix=PREFIX,\n",
" start_after=filename\n",
" ).data.objects\n",
"\n",
" if objects:\n",
" obj_name = objects[0].name\n",
" resp = object_storage_client.get_object(\n",
" namespace_name=NAMESPACE,\n",
" bucket_name=BUCKET,\n",
" object_name=obj_name\n",
" )\n",
" content = resp.data.content.decode(\"utf-8\")\n",
" process_and_display(content)\n",
" print(f\"File: {obj_name}\")\n",
" # print(content)\n",
"\n",
" time.sleep(1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "27638749",
"metadata": {},
"outputs": [],
"source": [
"if __name__ == \"__main__\":\n",
" main()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
109 changes: 109 additions & 0 deletions ai_services/vision/python/stream-video/stream-process-output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import time
import base64
from PIL import Image
from io import BytesIO
import cv2
import numpy as np
import json
import oci
from oci.object_storage import ObjectStorageClient


NAMESPACE = ""
BUCKET = ""
PREFIX = ""
CONFIG_PROFILE = ""


def decode_image(image_data):
"""Decode base64 image to OpenCV format and ensure it has 3 channels (RGB)."""
image_bytes = base64.b64decode(image_data)
image = Image.open(BytesIO(image_bytes))

if image.mode not in ("RGB", "RGBA"):
image = image.convert("RGB")

return cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)


def process_and_display(message):
"""Process detection data and update the window."""
message = message.replace("'", '"') # Make it valid JSON
message_dict = json.loads(message)

image = decode_image(message_dict['imageData'])
if len(message_dict['detectedObjects']) > 0:
face_id = message_dict['detectedObjects'][0]['objectId']
print("face detected", face_id)

print(message_dict['detectedObjects'])
# Uncomment if you want to visualize
annotated_image = draw_faces(image, message_dict['detectedObjects'], message_dict["imageData"])
cv2.imshow("Face Detection", annotated_image)
cv2.waitKey(1)


def draw_faces(image, faces, frame):
"""Draw bounding boxes around detected faces."""
height, width, _ = image.shape
for face in faces:
if face['name'] == 'weapon':
vertices = face['boundingPolygon']['normalizedVertices']
pts = [(int(v['x'] * width), int(v['y'] * height)) for v in vertices]
cv2.polylines(image, [np.array(pts)], isClosed=True, color=(0, 255, 0), thickness=2)
if frame is not None:
# Add frame number text in the top right corner
text = f"Frame: {frame}"
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 1
color = (0, 255, 0)
thickness = 1
text_size = cv2.getTextSize(text, font, font_scale, thickness)[0]
text_x = width - text_size[0] - 10
text_y = 30
cv2.putText(image, text, (text_x, text_y), font, font_scale, color, thickness)

return image


def main():
# Configure OCI client
config = oci.config.from_file('~/.oci/config', profile_name=CONFIG_PROFILE)
oendpoint = "https://objectstorage.us-ashburn-1.oraclecloud.com"

token_file = config['security_token_file']
with open(token_file, 'r') as f:
token = f.read()

private_key = oci.signer.load_private_key_from_file(config['key_file'])
signer = oci.auth.signers.SecurityTokenSigner(token, private_key)
object_storage_client = ObjectStorageClient(config=config, signer=signer, service_endpoint=oendpoint)

while True:
ts_ns = int(time.time() * 1_000_000_000) - 3_000_000_000
filename = f"new1/frame_{ts_ns}.json"

objects = object_storage_client.list_objects(
namespace_name=NAMESPACE,
bucket_name=BUCKET,
prefix=PREFIX,
start_after=filename
).data.objects

if objects:
obj_name = objects[0].name
resp = object_storage_client.get_object(
namespace_name=NAMESPACE,
bucket_name=BUCKET,
object_name=obj_name
)
content = resp.data.content.decode("utf-8")
process_and_display(content)
print(f"File: {obj_name}")
# print(content)

time.sleep(1)


if __name__ == "__main__":
main()
Loading