0% found this document useful (0 votes)
8 views

Trash Detection

Uploaded by

Vinay Jadhav
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Trash Detection

Uploaded by

Vinay Jadhav
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 17

from google.

colab import drive


drive.mount('/content/drive')

Mounted at /content/drive

import numpy as np
import pandas as pd
import tensorflow as tf
from PIL import Image
from matplotlib import pyplot as plt
from tensorflow.python.util import compat
from tensorflow.core.protobuf import saved_model_pb2
from google.protobuf import text_format
import pprint
import json
import os

# needed to install object_detection library and enlarge labels


! rm -rf ./models && git clone https://github.com/tensorflow/models.git \
&& sed -i "s#ImageFont.truetype('arial.ttf',
24)#ImageFont.truetype('arial.ttf', 50)#g"
./models/research/object_detection/utils/visualization_utils.py \
&& cp /usr/share/fonts/truetype/dejavu/DejaVuSans.ttf
/usr/share/fonts/truetype/dejavu/arial.ttf

Cloning into 'models'...


remote: Enumerating objects: 97166, done.ote: Counting objects: 100% (446/446),
done.ote: Compressing objects: 100% (211/211), done.ote: Total 97166 (delta 258),
reused 392 (delta 234), pack-reused 96720

# install object_detection library


! cd models/research \
&& protoc object_detection/protos/*.proto --python_out=. \
&& cp object_detection/packages/tf2/setup.py . && \
pip install .

Processing /content/models/research
Preparing metadata (setup.py) ... object-detection==0.1)
Downloading avro-python3-1.10.2.tar.gz (38 kB)
Preparing metadata (setup.py) ... (from object-detection==0.1)
Downloading apache_beam-2.56.0-cp310-cp310-
manylinux_2_17_x86_64.manylinux2014_x86_64.whl (14.5 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 14.5/14.5 MB 45.3 MB/s eta 0:00:00
ent already satisfied: pillow in /usr/local/lib/python3.10/dist-packages (from
object-detection==0.1) (9.4.0)
Requirement already satisfied: lxml in /usr/local/lib/python3.10/dist-packages
(from object-detection==0.1) (4.9.4)
Requirement already satisfied: matplotlib in /usr/local/lib/python3.10/dist-
packages (from object-detection==0.1) (3.7.1)
Requirement already satisfied: Cython in /usr/local/lib/python3.10/dist-
packages (from object-detection==0.1) (3.0.10)
Requirement already satisfied: contextlib2 in /usr/local/lib/python3.10/dist-
packages (from object-detection==0.1) (21.6.0)
Requirement already satisfied: tf-slim in /usr/local/lib/python3.10/dist-
packages (from object-detection==0.1) (1.1.0)
Requirement already satisfied: six in /usr/local/lib/python3.10/dist-packages
(from object-detection==0.1) (1.16.0)
Requirement already satisfied: pycocotools in /usr/local/lib/python3.10/dist-
packages (from object-detection==0.1) (2.0.7)
Collecting lvis (from object-detection==0.1)
Downloading lvis-0.5.3-py3-none-any.whl (14 kB)
Requirement already satisfied: scipy in /usr/local/lib/python3.10/dist-packages
(from object-detection==0.1) (1.11.4)
Requirement already satisfied: pandas in /usr/local/lib/python3.10/dist-
packages (from object-detection==0.1) (2.0.3)
Collecting tf-models-official>=2.5.1 (from object-detection==0.1)
Downloading tf_models_official-2.16.0-py2.py3-none-any.whl (2.7 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.7/2.7 MB 60.0 MB/s eta 0:00:00
object-detection==0.1)
Downloading tensorflow_io-0.37.0-cp310-cp310-
manylinux_2_17_x86_64.manylinux2014_x86_64.whl (49.6 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 49.6/49.6 MB 8.7 MB/s eta 0:00:00
ent already satisfied: keras in /usr/local/lib/python3.10/dist-packages (from
object-detection==0.1) (2.15.0)
Collecting pyparsing==2.4.7 (from object-detection==0.1)
Downloading pyparsing-2.4.7-py2.py3-none-any.whl (67 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 67.8/67.8 kB 9.0 MB/s eta 0:00:00
object-detection==0.1)
Downloading sacrebleu-2.2.0-py3-none-any.whl (116 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 116.6/116.6 kB 14.2 MB/s eta 0:00:00
sacrebleu<=2.2.0->object-detection==0.1)
Downloading portalocker-2.8.2-py3-none-any.whl (17 kB)
Requirement already satisfied: regex in /usr/local/lib/python3.10/dist-packages
(from sacrebleu<=2.2.0->object-detection==0.1) (2023.12.25)
Requirement already satisfied: tabulate>=0.8.9 in
/usr/local/lib/python3.10/dist-packages (from sacrebleu<=2.2.0->object-
detection==0.1) (0.9.0)
Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.10/dist-
packages (from sacrebleu<=2.2.0->object-detection==0.1) (1.25.2)
Collecting colorama (from sacrebleu<=2.2.0->object-detection==0.1)
Downloading colorama-0.4.6-py2.py3-none-any.whl (25 kB)
Requirement already satisfied: gin-config in /usr/local/lib/python3.10/dist-
packages (from tf-models-official>=2.5.1->object-detection==0.1) (0.5.0)
Requirement already satisfied: google-api-python-client>=1.6.7 in
/usr/local/lib/python3.10/dist-packages (from tf-models-official>=2.5.1->object-
detection==0.1) (2.84.0)
Collecting immutabledict (from tf-models-official>=2.5.1->object-
detection==0.1)
Downloading immutabledict-4.2.0-py3-none-any.whl (4.7 kB)
Requirement already satisfied: kaggle>=1.3.9 in /usr/local/lib/python3.10/dist-
packages (from tf-models-official>=2.5.1->object-detection==0.1) (1.6.12)
Requirement already satisfied: oauth2client in /usr/local/lib/python3.10/dist-
packages (from tf-models-official>=2.5.1->object-detection==0.1) (4.1.3)
Requirement already satisfied: opencv-python-headless in
/usr/local/lib/python3.10/dist-packages (from tf-models-official>=2.5.1->object-
detection==0.1) (4.9.0.80)
Requirement already satisfied: psutil>=5.4.3 in /usr/local/lib/python3.10/dist-
packages (from tf-models-official>=2.5.1->object-detection==0.1) (5.9.5)
Requirement already satisfied: py-cpuinfo>=3.3.0 in
/usr/local/lib/python3.10/dist-packages (from tf-models-official>=2.5.1->object-
detection==0.1) (9.0.0)
Requirement already satisfied: pyyaml>=6.0.0 in /usr/local/lib/python3.10/dist-
packages (from tf-models-official>=2.5.1->object-detection==0.1) (6.0.1)
Requirement already satisfied: sentencepiece in /usr/local/lib/python3.10/dist-
packages (from tf-models-official>=2.5.1->object-detection==0.1) (0.1.99)
Collecting seqeval (from tf-models-official>=2.5.1->object-detection==0.1)
Downloading seqeval-1.2.2.tar.gz (43 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 43.6/43.6 kB 5.0 MB/s eta 0:00:00
etadata (setup.py) ... ent already satisfied: tensorflow-datasets in
/usr/local/lib/python3.10/dist-packages (from tf-models-official>=2.5.1->object-
detection==0.1) (4.9.4)
Requirement already satisfied: tensorflow-hub>=0.6.0 in
/usr/local/lib/python3.10/dist-packages (from tf-models-official>=2.5.1->object-
detection==0.1) (0.16.1)
Collecting tensorflow-model-optimization>=0.4.1 (from tf-models-
official>=2.5.1->object-detection==0.1)
Downloading tensorflow_model_optimization-0.8.0-py2.py3-none-any.whl (242 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 242.5/242.5 kB 30.9 MB/s eta 0:00:00
tf-models-official>=2.5.1->object-detection==0.1)
Downloading tensorflow_text-2.16.1-cp310-cp310-
manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.2 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.2/5.2 MB 93.7 MB/s eta 0:00:00
tf-models-official>=2.5.1->object-detection==0.1)
Downloading tensorflow-2.16.1-cp310-cp310-
manylinux_2_17_x86_64.manylinux2014_x86_64.whl (589.8 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 589.8/589.8 MB 1.2 MB/s eta 0:00:00
tf-models-official>=2.5.1->object-detection==0.1)
Downloading tf_keras-2.16.0-py3-none-any.whl (1.7 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.7/1.7 MB 80.7 MB/s eta 0:00:00
ent already satisfied: python-dateutil>=2.8.2 in
/usr/local/lib/python3.10/dist-packages (from pandas->object-detection==0.1)
(2.8.2)
Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-
packages (from pandas->object-detection==0.1) (2023.4)
Requirement already satisfied: tzdata>=2022.1 in
/usr/local/lib/python3.10/dist-packages (from pandas->object-detection==0.1)
(2024.1)
Requirement already satisfied: absl-py>=0.2.2 in
/usr/local/lib/python3.10/dist-packages (from tf-slim->object-detection==0.1)
(1.4.0)
Collecting crcmod<2.0,>=1.7 (from apache-beam->object-detection==0.1)
Downloading crcmod-1.7.tar.gz (89 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 89.7/89.7 kB 12.3 MB/s eta 0:00:00
etadata (setup.py) ... apache-beam->object-detection==0.1)
Downloading orjson-3.10.3-cp310-cp310-
manylinux_2_17_x86_64.manylinux2014_x86_64.whl (142 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 142.5/142.5 kB 17.9 MB/s eta 0:00:00
apache-beam->object-detection==0.1)
Downloading dill-0.3.1.1.tar.gz (151 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 152.0/152.0 kB 20.1 MB/s eta 0:00:00
etadata (setup.py) ... ent already satisfied: cloudpickle~=2.2.1 in
/usr/local/lib/python3.10/dist-packages (from apache-beam->object-detection==0.1)
(2.2.1)
Collecting fastavro<2,>=0.23.6 (from apache-beam->object-detection==0.1)
Downloading fastavro-1.9.4-cp310-cp310-
manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.1/3.1 MB 64.6 MB/s eta 0:00:00
apache-beam->object-detection==0.1)
Downloading fasteners-0.19-py3-none-any.whl (18 kB)
Requirement already satisfied: grpcio!=1.48.0,!=1.59.*,!=1.60.*,!=1.61.*,!
=1.62.0,!=1.62.1,<2,>=1.33.1 in /usr/local/lib/python3.10/dist-packages (from
apache-beam->object-detection==0.1) (1.63.0)
Collecting hdfs<3.0.0,>=2.1.0 (from apache-beam->object-detection==0.1)
Downloading hdfs-2.7.3.tar.gz (43 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 43.5/43.5 kB 5.5 MB/s eta 0:00:00
etadata (setup.py) ... ent already satisfied: httplib2<0.23.0,>=0.8 in
/usr/local/lib/python3.10/dist-packages (from apache-beam->object-detection==0.1)
(0.22.0)
Requirement already satisfied: jsonschema<5.0.0,>=4.0.0 in
/usr/local/lib/python3.10/dist-packages (from apache-beam->object-detection==0.1)
(4.19.2)
Requirement already satisfied: jsonpickle<4.0.0,>=3.0.0 in
/usr/local/lib/python3.10/dist-packages (from apache-beam->object-detection==0.1)
(3.0.4)
Collecting objsize<0.8.0,>=0.6.1 (from apache-beam->object-detection==0.1)
Downloading objsize-0.7.0-py3-none-any.whl (11 kB)
Requirement already satisfied: packaging>=22.0 in
/usr/local/lib/python3.10/dist-packages (from apache-beam->object-detection==0.1)
(24.0)
Collecting pymongo<5.0.0,>=3.8.0 (from apache-beam->object-detection==0.1)
Downloading pymongo-4.7.1-cp310-cp310-
manylinux_2_17_x86_64.manylinux2014_x86_64.whl (669 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 670.0/670.0 kB 40.8 MB/s eta 0:00:00
ent already satisfied: proto-plus<2,>=1.7.1 in /usr/local/lib/python3.10/dist-
packages (from apache-beam->object-detection==0.1) (1.23.0)
Requirement already satisfied: protobuf!=4.0.*,!=4.21.*,!=4.22.0,!=4.23.*,!
=4.24.*,<4.26.0,>=3.20.3 in /usr/local/lib/python3.10/dist-packages (from apache-
beam->object-detection==0.1) (3.20.3)
Requirement already satisfied: pydot<2,>=1.2.0 in
/usr/local/lib/python3.10/dist-packages (from apache-beam->object-detection==0.1)
(1.4.2)
Collecting redis<6,>=5.0.0 (from apache-beam->object-detection==0.1)
Downloading redis-5.0.4-py3-none-any.whl (251 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 252.0/252.0 kB 24.4 MB/s eta 0:00:00
ent already satisfied: requests<3.0.0,>=2.24.0 in
/usr/local/lib/python3.10/dist-packages (from apache-beam->object-detection==0.1)
(2.31.0)
Requirement already satisfied: typing-extensions>=3.7.0 in
/usr/local/lib/python3.10/dist-packages (from apache-beam->object-detection==0.1)
(4.11.0)
Collecting zstandard<1,>=0.18.0 (from apache-beam->object-detection==0.1)
Downloading zstandard-0.22.0-cp310-cp310-
manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.4 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.4/5.4 MB 68.1 MB/s eta 0:00:00
ent already satisfied: pyarrow<15.0.0,>=3.0.0 in
/usr/local/lib/python3.10/dist-packages (from apache-beam->object-detection==0.1)
(14.0.2)
Requirement already satisfied: pyarrow-hotfix<1 in
/usr/local/lib/python3.10/dist-packages (from apache-beam->object-detection==0.1)
(0.6)
Collecting js2py<1,>=0.74 (from apache-beam->object-detection==0.1)
Downloading Js2Py-0.74-py3-none-any.whl (1.0 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.0/1.0 MB 47.4 MB/s eta 0:00:00
ent already satisfied: cycler>=0.10.0 in /usr/local/lib/python3.10/dist-
packages (from lvis->object-detection==0.1) (0.12.1)
Requirement already satisfied: kiwisolver>=1.1.0 in
/usr/local/lib/python3.10/dist-packages (from lvis->object-detection==0.1) (1.4.5)
Requirement already satisfied: opencv-python>=4.1.0.25 in
/usr/local/lib/python3.10/dist-packages (from lvis->object-detection==0.1)
(4.8.0.76)
Requirement already satisfied: contourpy>=1.0.1 in
/usr/local/lib/python3.10/dist-packages (from matplotlib->object-detection==0.1)
(1.2.1)
Requirement already satisfied: fonttools>=4.22.0 in
/usr/local/lib/python3.10/dist-packages (from matplotlib->object-detection==0.1)
(4.51.0)
Requirement already satisfied: tensorflow-io-gcs-filesystem==0.37.0 in
/usr/local/lib/python3.10/dist-packages (from tensorflow_io->object-detection==0.1)
(0.37.0)
Requirement already satisfied: google-auth<3.0.0dev,>=1.19.0 in
/usr/local/lib/python3.10/dist-packages (from google-api-python-client>=1.6.7->tf-
models-official>=2.5.1->object-detection==0.1) (2.27.0)
Requirement already satisfied: google-auth-httplib2>=0.1.0 in
/usr/local/lib/python3.10/dist-packages (from google-api-python-client>=1.6.7->tf-
models-official>=2.5.1->object-detection==0.1) (0.1.1)
Requirement already satisfied: google-api-core!=2.0.*,!=2.1.*,!=2.2.*,!
=2.3.0,<3.0.0dev,>=1.31.5 in /usr/local/lib/python3.10/dist-packages (from google-
api-python-client>=1.6.7->tf-models-official>=2.5.1->object-detection==0.1)
(2.11.1)
Requirement already satisfied: uritemplate<5,>=3.0.1 in
/usr/local/lib/python3.10/dist-packages (from google-api-python-client>=1.6.7->tf-
models-official>=2.5.1->object-detection==0.1) (4.1.1)
Collecting docopt (from hdfs<3.0.0,>=2.1.0->apache-beam->object-detection==0.1)
Downloading docopt-0.6.2.tar.gz (25 kB)
Preparing metadata (setup.py) ... ent already satisfied: tzlocal>=1.2 in
/usr/local/lib/python3.10/dist-packages (from js2py<1,>=0.74->apache-beam->object-
detection==0.1) (5.2)
Collecting pyjsparser>=2.5.1 (from js2py<1,>=0.74->apache-beam->object-
detection==0.1)
Downloading pyjsparser-2.7.1.tar.gz (24 kB)
Preparing metadata (setup.py) ... ent already satisfied: attrs>=22.2.0 in
/usr/local/lib/python3.10/dist-packages (from jsonschema<5.0.0,>=4.0.0->apache-
beam->object-detection==0.1) (23.2.0)
Requirement already satisfied: jsonschema-specifications>=2023.03.6 in
/usr/local/lib/python3.10/dist-packages (from jsonschema<5.0.0,>=4.0.0->apache-
beam->object-detection==0.1) (2023.12.1)
Requirement already satisfied: referencing>=0.28.4 in
/usr/local/lib/python3.10/dist-packages (from jsonschema<5.0.0,>=4.0.0->apache-
beam->object-detection==0.1) (0.35.0)
Requirement already satisfied: rpds-py>=0.7.1 in
/usr/local/lib/python3.10/dist-packages (from jsonschema<5.0.0,>=4.0.0->apache-
beam->object-detection==0.1) (0.18.0)
Requirement already satisfied: certifi>=2023.7.22 in
/usr/local/lib/python3.10/dist-packages (from kaggle>=1.3.9->tf-models-
official>=2.5.1->object-detection==0.1) (2024.2.2)
Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages
(from kaggle>=1.3.9->tf-models-official>=2.5.1->object-detection==0.1) (4.66.2)
Requirement already satisfied: python-slugify in
/usr/local/lib/python3.10/dist-packages (from kaggle>=1.3.9->tf-models-
official>=2.5.1->object-detection==0.1) (8.0.4)
Requirement already satisfied: urllib3 in /usr/local/lib/python3.10/dist-
packages (from kaggle>=1.3.9->tf-models-official>=2.5.1->object-detection==0.1)
(2.0.7)
Requirement already satisfied: bleach in /usr/local/lib/python3.10/dist-
packages (from kaggle>=1.3.9->tf-models-official>=2.5.1->object-detection==0.1)
(6.1.0)
Collecting dnspython<3.0.0,>=1.16.0 (from pymongo<5.0.0,>=3.8.0->apache-beam-
>object-detection==0.1)
Downloading dnspython-2.6.1-py3-none-any.whl (307 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 307.7/307.7 kB 29.7 MB/s eta 0:00:00
ent already satisfied: async-timeout>=4.0.3 in /usr/local/lib/python3.10/dist-
packages (from redis<6,>=5.0.0->apache-beam->object-detection==0.1) (4.0.3)
Requirement already satisfied: charset-normalizer<4,>=2 in
/usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.24.0->apache-beam-
>object-detection==0.1) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-
packages (from requests<3.0.0,>=2.24.0->apache-beam->object-detection==0.1) (3.7)
Requirement already satisfied: astunparse>=1.6.0 in
/usr/local/lib/python3.10/dist-packages (from tensorflow~=2.16.1->tf-models-
official>=2.5.1->object-detection==0.1) (1.6.3)
Requirement already satisfied: flatbuffers>=23.5.26 in
/usr/local/lib/python3.10/dist-packages (from tensorflow~=2.16.1->tf-models-
official>=2.5.1->object-detection==0.1) (24.3.25)
Requirement already satisfied: gast!=0.5.0,!=0.5.1,!=0.5.2,>=0.2.1 in
/usr/local/lib/python3.10/dist-packages (from tensorflow~=2.16.1->tf-models-
official>=2.5.1->object-detection==0.1) (0.5.4)
Requirement already satisfied: google-pasta>=0.1.1 in
/usr/local/lib/python3.10/dist-packages (from tensorflow~=2.16.1->tf-models-
official>=2.5.1->object-detection==0.1) (0.2.0)
Collecting h5py>=3.10.0 (from tensorflow~=2.16.1->tf-models-official>=2.5.1-
>object-detection==0.1)
Downloading h5py-3.11.0-cp310-cp310-
manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.3 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.3/5.3 MB 100.1 MB/s eta 0:00:00
ent already satisfied: libclang>=13.0.0 in /usr/local/lib/python3.10/dist-
packages (from tensorflow~=2.16.1->tf-models-official>=2.5.1->object-
detection==0.1) (18.1.1)
Collecting ml-dtypes~=0.3.1 (from tensorflow~=2.16.1->tf-models-
official>=2.5.1->object-detection==0.1)
Downloading ml_dtypes-0.3.2-cp310-cp310-
manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.2 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.2/2.2 MB 92.3 MB/s eta 0:00:00
ent already satisfied: opt-einsum>=2.3.2 in /usr/local/lib/python3.10/dist-
packages (from tensorflow~=2.16.1->tf-models-official>=2.5.1->object-
detection==0.1) (3.3.0)
Requirement already satisfied: setuptools in /usr/local/lib/python3.10/dist-
packages (from tensorflow~=2.16.1->tf-models-official>=2.5.1->object-
detection==0.1) (67.7.2)
Requirement already satisfied: termcolor>=1.1.0 in
/usr/local/lib/python3.10/dist-packages (from tensorflow~=2.16.1->tf-models-
official>=2.5.1->object-detection==0.1) (2.4.0)
Requirement already satisfied: wrapt>=1.11.0 in /usr/local/lib/python3.10/dist-
packages (from tensorflow~=2.16.1->tf-models-official>=2.5.1->object-
detection==0.1) (1.14.1)
Collecting tensorboard<2.17,>=2.16 (from tensorflow~=2.16.1->tf-models-
official>=2.5.1->object-detection==0.1)
Downloading tensorboard-2.16.2-py3-none-any.whl (5.5 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.5/5.5 MB 104.8 MB/s eta 0:00:00
object-detection==0.1)
Downloading keras-3.3.3-py3-none-any.whl (1.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.1/1.1 MB 71.6 MB/s eta 0:00:00
ent already satisfied: rich in /usr/local/lib/python3.10/dist-packages (from
keras->object-detection==0.1) (13.7.1)
Collecting namex (from keras->object-detection==0.1)
Downloading namex-0.0.8-py3-none-any.whl (5.8 kB)
Collecting optree (from keras->object-detection==0.1)
Downloading optree-0.11.0-cp310-cp310-
manylinux_2_17_x86_64.manylinux2014_x86_64.whl (311 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 311.2/311.2 kB 37.1 MB/s eta 0:00:00
ent already satisfied: dm-tree~=0.1.1 in /usr/local/lib/python3.10/dist-
packages (from tensorflow-model-optimization>=0.4.1->tf-models-official>=2.5.1-
>object-detection==0.1) (0.1.8)
Requirement already satisfied: pyasn1>=0.1.7 in /usr/local/lib/python3.10/dist-
packages (from oauth2client->tf-models-official>=2.5.1->object-detection==0.1)
(0.6.0)
Requirement already satisfied: pyasn1-modules>=0.0.5 in
/usr/local/lib/python3.10/dist-packages (from oauth2client->tf-models-
official>=2.5.1->object-detection==0.1) (0.4.0)
Requirement already satisfied: rsa>=3.1.4 in /usr/local/lib/python3.10/dist-
packages (from oauth2client->tf-models-official>=2.5.1->object-detection==0.1)
(4.9)
Requirement already satisfied: markdown-it-py>=2.2.0 in
/usr/local/lib/python3.10/dist-packages (from rich->keras->object-detection==0.1)
(3.0.0)
Requirement already satisfied: pygments<3.0.0,>=2.13.0 in
/usr/local/lib/python3.10/dist-packages (from rich->keras->object-detection==0.1)
(2.16.1)
Requirement already satisfied: scikit-learn>=0.21.3 in
/usr/local/lib/python3.10/dist-packages (from seqeval->tf-models-official>=2.5.1-
>object-detection==0.1) (1.2.2)
Requirement already satisfied: click in /usr/local/lib/python3.10/dist-packages
(from tensorflow-datasets->tf-models-official>=2.5.1->object-detection==0.1)
(8.1.7)
Requirement already satisfied: etils[enp,epath,etree]>=0.9.0 in
/usr/local/lib/python3.10/dist-packages (from tensorflow-datasets->tf-models-
official>=2.5.1->object-detection==0.1) (1.7.0)
Requirement already satisfied: promise in /usr/local/lib/python3.10/dist-
packages (from tensorflow-datasets->tf-models-official>=2.5.1->object-
detection==0.1) (2.3)
Requirement already satisfied: tensorflow-metadata in
/usr/local/lib/python3.10/dist-packages (from tensorflow-datasets->tf-models-
official>=2.5.1->object-detection==0.1) (1.15.0)
Requirement already satisfied: toml in /usr/local/lib/python3.10/dist-packages
(from tensorflow-datasets->tf-models-official>=2.5.1->object-detection==0.1)
(0.10.2)
Requirement already satisfied: array-record>=0.5.0 in
/usr/local/lib/python3.10/dist-packages (from tensorflow-datasets->tf-models-
official>=2.5.1->object-detection==0.1) (0.5.1)
Requirement already satisfied: wheel<1.0,>=0.23.0 in
/usr/local/lib/python3.10/dist-packages (from astunparse>=1.6.0-
>tensorflow~=2.16.1->tf-models-official>=2.5.1->object-detection==0.1) (0.43.0)
Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-
packages (from etils[enp,epath,etree]>=0.9.0->tensorflow-datasets->tf-models-
official>=2.5.1->object-detection==0.1) (2023.6.0)
Requirement already satisfied: importlib_resources in
/usr/local/lib/python3.10/dist-packages (from etils[enp,epath,etree]>=0.9.0-
>tensorflow-datasets->tf-models-official>=2.5.1->object-detection==0.1) (6.4.0)
Requirement already satisfied: zipp in /usr/local/lib/python3.10/dist-packages
(from etils[enp,epath,etree]>=0.9.0->tensorflow-datasets->tf-models-
official>=2.5.1->object-detection==0.1) (3.18.1)
Requirement already satisfied: googleapis-common-protos<2.0.dev0,>=1.56.2 in
/usr/local/lib/python3.10/dist-packages (from google-api-core!=2.0.*,!=2.1.*,!
=2.2.*,!=2.3.0,<3.0.0dev,>=1.31.5->google-api-python-client>=1.6.7->tf-models-
official>=2.5.1->object-detection==0.1) (1.63.0)
Requirement already satisfied: cachetools<6.0,>=2.0.0 in
/usr/local/lib/python3.10/dist-packages (from google-auth<3.0.0dev,>=1.19.0-
>google-api-python-client>=1.6.7->tf-models-official>=2.5.1->object-detection==0.1)
(5.3.3)
Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.10/dist-
packages (from markdown-it-py>=2.2.0->rich->keras->object-detection==0.1) (0.1.2)
Requirement already satisfied: joblib>=1.1.1 in /usr/local/lib/python3.10/dist-
packages (from scikit-learn>=0.21.3->seqeval->tf-models-official>=2.5.1->object-
detection==0.1) (1.4.0)
Requirement already satisfied: threadpoolctl>=2.0.0 in
/usr/local/lib/python3.10/dist-packages (from scikit-learn>=0.21.3->seqeval->tf-
models-official>=2.5.1->object-detection==0.1) (3.5.0)
Requirement already satisfied: markdown>=2.6.8 in
/usr/local/lib/python3.10/dist-packages (from tensorboard<2.17,>=2.16-
>tensorflow~=2.16.1->tf-models-official>=2.5.1->object-detection==0.1) (3.6)
Requirement already satisfied: tensorboard-data-server<0.8.0,>=0.7.0 in
/usr/local/lib/python3.10/dist-packages (from tensorboard<2.17,>=2.16-
>tensorflow~=2.16.1->tf-models-official>=2.5.1->object-detection==0.1) (0.7.2)
Requirement already satisfied: werkzeug>=1.0.1 in
/usr/local/lib/python3.10/dist-packages (from tensorboard<2.17,>=2.16-
>tensorflow~=2.16.1->tf-models-official>=2.5.1->object-detection==0.1) (3.0.2)
Requirement already satisfied: webencodings in /usr/local/lib/python3.10/dist-
packages (from bleach->kaggle>=1.3.9->tf-models-official>=2.5.1->object-
detection==0.1) (0.5.1)
Requirement already satisfied: text-unidecode>=1.3 in
/usr/local/lib/python3.10/dist-packages (from python-slugify->kaggle>=1.3.9->tf-
models-official>=2.5.1->object-detection==0.1) (1.3)
Requirement already satisfied: MarkupSafe>=2.1.1 in
/usr/local/lib/python3.10/dist-packages (from werkzeug>=1.0.1-
>tensorboard<2.17,>=2.16->tensorflow~=2.16.1->tf-models-official>=2.5.1->object-
detection==0.1) (2.1.5)
Building wheels for collected packages: object-detection, avro-python3, crcmod,
dill, hdfs, seqeval, pyjsparser, docopt
Building wheel for object-detection (setup.py) ... e=object_detection-0.1-
py3-none-any.whl size=1697356
sha256=2508ab80a126a1b9a35729ac03205b392b261ac24506dabf4c37a7daae960938
Stored in directory:
/tmp/pip-ephem-wheel-cache-vnyqrbq_/wheels/53/dd/70/2de274d6c443c69d367bd6a5606f95e
5a6df61aacf1435ec0d
Building wheel for avro-python3 (setup.py) ... e=avro_python3-1.10.2-py3-
none-any.whl size=43992
sha256=3fa10affc24750f2ce98c160ebeffe008333b2730a0f9d269fa79c965a7861a8
Stored in directory:
/root/.cache/pip/wheels/bc/85/62/6cdd81c56f923946b401cecff38055b94c9b766927f7d8ca82
Building wheel for crcmod (setup.py) ... od: filename=crcmod-1.7-cp310-cp310-
linux_x86_64.whl size=31406
sha256=91fb460a741369ecdef93f9eef5e56d43352cb9bb0a947b8a14c78bfac083c1e
Stored in directory:
/root/.cache/pip/wheels/85/4c/07/72215c529bd59d67e3dac29711d7aba1b692f543c808ba9e86
Building wheel for dill (setup.py) ... e=dill-0.3.1.1-py3-none-any.whl
size=78541 sha256=a3350972b2b68e654783b924c87a6ceecb4ea610ebc608ea99d6117201e36481
Stored in directory:
/root/.cache/pip/wheels/ea/e2/86/64980d90e297e7bf2ce588c2b96e818f5399c515c4bb8a7e4f
Building wheel for hdfs (setup.py) ... e=hdfs-2.7.3-py3-none-any.whl
size=34324 sha256=58dd1efad7cb7dca890834e34bfe689bba94927849f4f01b39f86030bb6775e6
Stored in directory:
/root/.cache/pip/wheels/e5/8d/b6/99c1c0a3ac5788c866b0ecd3f48b0134a5910e6ed26011800b
Building wheel for seqeval (setup.py) ... e=seqeval-1.2.2-py3-none-any.whl
size=16161 sha256=7825fceb5abdc67daac9ee50db5045cac046fa2fb121cd39a8c88d9b9d948508
Stored in directory:
/root/.cache/pip/wheels/1a/67/4a/ad4082dd7dfc30f2abfe4d80a2ed5926a506eb8a972b4767fa
Building wheel for pyjsparser (setup.py) ... e=pyjsparser-2.7.1-py3-none-
any.whl size=25984
sha256=21bf64ad01b2c43a7453ba94eff5bcd7c44b5a81e805b7542af1843fbff7c79f
Stored in directory:
/root/.cache/pip/wheels/5e/81/26/5956478df303e2bf5a85a5df595bb307bd25948a4bab69f7c7
Building wheel for docopt (setup.py) ... e=docopt-0.6.2-py2.py3-none-any.whl
size=13706 sha256=250d5517e47a3b8883921f258793b522b5357bb195fd55642baa5abc24130cdc
Stored in directory:
/root/.cache/pip/wheels/fc/ab/d4/5da2067ac95b36618c629a5f93f809425700506f72c9732fac
Successfully built object-detection avro-python3 crcmod dill hdfs seqeval
pyjsparser docopt
Installing collected packages: pyjsparser, namex, docopt, crcmod, zstandard,
tensorflow-model-optimization, tensorflow_io, redis, pyparsing, portalocker,
orjson, optree, objsize, ml-dtypes, js2py, immutabledict, h5py, fasteners,
fastavro, dnspython, dill, colorama, avro-python3, tensorboard, sacrebleu, pymongo,
hdfs, seqeval, lvis, keras, tensorflow, apache-beam, tf-keras, tensorflow-text, tf-
models-official, object-detection
Attempting uninstall: pyparsing
Found existing installation: pyparsing 3.1.2
Uninstalling pyparsing-3.1.2:
Successfully uninstalled pyparsing-3.1.2
Attempting uninstall: ml-dtypes
Found existing installation: ml-dtypes 0.2.0
Uninstalling ml-dtypes-0.2.0:
Successfully uninstalled ml-dtypes-0.2.0
Attempting uninstall: h5py
Found existing installation: h5py 3.9.0
Uninstalling h5py-3.9.0:
Successfully uninstalled h5py-3.9.0
Attempting uninstall: tensorboard
Found existing installation: tensorboard 2.15.2
Uninstalling tensorboard-2.15.2:
Successfully uninstalled tensorboard-2.15.2
Attempting uninstall: keras
Found existing installation: keras 2.15.0
Uninstalling keras-2.15.0:
Successfully uninstalled keras-2.15.0
Attempting uninstall: tensorflow
Found existing installation: tensorflow 2.15.0
Uninstalling tensorflow-2.15.0:
Successfully uninstalled tensorflow-2.15.0
Attempting uninstall: tf-keras
Found existing installation: tf_keras 2.15.1
Uninstalling tf_keras-2.15.1:
Successfully uninstalled tf_keras-2.15.1
Successfully installed apache-beam-2.56.0 avro-python3-1.10.2 colorama-0.4.6
crcmod-1.7 dill-0.3.1.1 dnspython-2.6.1 docopt-0.6.2 fastavro-1.9.4 fasteners-0.19
h5py-3.11.0 hdfs-2.7.3 immutabledict-4.2.0 js2py-0.74 keras-3.3.3 lvis-0.5.3 ml-
dtypes-0.3.2 namex-0.0.8 object-detection-0.1 objsize-0.7.0 optree-0.11.0 orjson-
3.10.3 portalocker-2.8.2 pyjsparser-2.7.1 pymongo-4.7.1 pyparsing-2.4.7 redis-5.0.4
sacrebleu-2.2.0 seqeval-1.2.2 tensorboard-2.16.2 tensorflow-2.16.1 tensorflow-
model-optimization-0.8.0 tensorflow-text-2.16.1 tensorflow_io-0.37.0 tf-keras-
2.16.0 tf-models-official-2.16.0 zstandard-0.22.0

from object_detection.utils import visualization_utils as vis_util


from object_detection.utils import dataset_util, label_map_util
from object_detection.protos import string_int_label_map_pb2

# reconstruct frozen graph


def reconstruct(pb_path):
if not os.path.isfile(pb_path):
print("Error: %s not found" % pb_path)

print("Reconstructing Tensorflow model")


detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.compat.v1.GraphDef()
with tf.io.gfile.GFile(pb_path, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
print("Success!")
return detection_graph

# visualize detection
def image2np(image):
(w, h) = image.size
return np.array(image.getdata()).reshape((h, w, 3)).astype(np.uint8)

def image2tensor(image):
npim = image2np(image)
return np.expand_dims(npim, axis=0)

# %matplotlib inline
# def detect(detection_graph, test_image_path):
# with detection_graph.as_default():
# gpu_options =
tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction=0.01)
# with
tf.compat.v1.Session(graph=detection_graph,config=tf.compat.v1.ConfigProto(gpu_opti
ons=gpu_options)) as sess:
# image_tensor =
detection_graph.get_tensor_by_name('image_tensor:0')
# detection_boxes =
detection_graph.get_tensor_by_name('detection_boxes:0')
# detection_scores =
detection_graph.get_tensor_by_name('detection_scores:0')
# detection_classes =
detection_graph.get_tensor_by_name('detection_classes:0')
# num_detections =
detection_graph.get_tensor_by_name('num_detections:0')
# print(detection_classes)
# image = Image.open(test_image_path)
# (boxes, scores, classes, num) = sess.run(
# [detection_boxes, detection_scores, detection_classes,
num_detections],
# feed_dict={image_tensor: image2tensor(image)}
# )

# npim = image2np(image)
# vis_util.visualize_boxes_and_labels_on_image_array(
# npim,
# np.squeeze(boxes),
# np.squeeze(classes).astype(np.int32),
# np.squeeze(scores),
# category_index,
# use_normalized_coordinates=True,
# line_thickness=15)
# plt.figure(figsize=(12, 8))
# plt.imshow(npim)
# plt.show()

label_map =
label_map_util.load_labelmap('/content/drive/MyDrive/sunday/labelmap.pbtxt')
categories = label_map_util.convert_label_map_to_categories(label_map,
max_num_classes=60, use_display_name=True)
category_index = label_map_util.create_category_index(categories)

detection_graph =
reconstruct("/content/drive/MyDrive/sunday/ssd_mobilenet_v2_taco_2018_03_29.pb")

Reconstructing Tensorflow model


Success!

# detect(detection_graph,
'/content/drive/MyDrive/kaggle1/kaggle/train/000000_JPG_jpg.rf.1fe2fd57e7442d2d151e
149a00fa7cf7.jpg')

Tensor("detection_classes:0", dtype=float32)

[]

def detect(detection_graph, frame):


with detection_graph.as_default():
with tf.compat.v1.Session(graph=detection_graph) as sess:
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
detection_boxes =
detection_graph.get_tensor_by_name('detection_boxes:0')
detection_scores =
detection_graph.get_tensor_by_name('detection_scores:0')
detection_classes =
detection_graph.get_tensor_by_name('detection_classes:0')
num_detections =
detection_graph.get_tensor_by_name('num_detections:0')
(boxes, scores, classes, num) = sess.run(
[detection_boxes, detection_scores, detection_classes,
num_detections],
feed_dict={image_tensor: image2tensor(frame)}
)
npim = image2np(frame)
vis_util.visualize_boxes_and_labels_on_image_array(
npim,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=15)
return npim

# import cv2
# from google.colab.patches import cv2_imshow
# # Open the input video
# # Open the input video
# cap = cv2.VideoCapture('/content/VID20240505155419.mp4')

# # Get video properties


# fps = cap.get(cv2.CAP_PROP_FPS)

# img=[]
# # Process each frame of the video
# while True:
# ret, frame = cap.read()
# if frame is not None:
# # Perform object detection on the frame
# frame_with_detections = detect(detection_graph, Image.fromarray(frame))

# # Write the frame with detection results to the output video


# # out.write(cv2.cvtColor(frame_with_detections, cv2.COLOR_RGB2BGR))
# img.append(cv2.cvtColor(frame_with_detections, cv2.COLOR_RGB2BGR))

# # Display the frame


# cv2_imshow(cv2.cvtColor(frame_with_detections, cv2.COLOR_RGB2BGR))

# if not ret:
# break

# # Release video capture and writer


# cap.release()
# height,width,layers=img[1].shape
#
out=cv2.VideoWriter('/content/output.mp4',cv2.VideoWriter_fourcc(*'mp4v'),fps,
(width,height))
# for i in range(len(img)):
# out.write(img[i])

# # video.release()
# out.release()

# # Close all OpenCV windows


# cv2.destroyAllWindows()

# import cv2
# from google.colab.patches import cv2_imshow

# # Open the webcam


# cap = cv2.VideoCapture(0) # Change 0 to the index of your webcam if it's not
the default one

# # Get the fps of the webcam


# fps = cap.get(cv2.CAP_PROP_FPS)

# img = []

# # Process each frame from the webcam


# while True:
# ret, frame = cap.read()
# if ret:
# # Perform object detection on the frame
# frame_with_detections = detect(detection_graph,
Image.fromarray(frame))

# # Display the frame


# cv2_imshow(cv2.cvtColor(frame_with_detections, cv2.COLOR_RGB2BGR))

# # Store the frame with detection results


# img.append(cv2.cvtColor(frame_with_detections, cv2.COLOR_RGB2BGR))

# # Check for key press to exit


# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
# # Release the webcam
# cap.release()

# # Write the frames with detection results to an output video file


# if img:
# height, width, layers = img[0].shape
# out = cv2.VideoWriter('/content/output.mp4',
cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
# for i in range(len(img)):
# out.write(img[i])
# out.release()

# # Close all OpenCV windows


# cv2.destroyAllWindows()

# import dependencies
from IPython.display import display, Javascript, Image
from google.colab.output import eval_js
from google.colab.patches import cv2_imshow
from base64 import b64decode, b64encode
import cv2
import numpy as np
import PIL
import io
import html
import time
import matplotlib.pyplot as plt
%matplotlib inline

# function to convert the JavaScript object into an OpenCV image


def js_to_image(js_reply):
"""
Params:
js_reply: JavaScript object containing image from webcam
Returns:
img: OpenCV BGR image
"""
# decode base64 image
image_bytes = b64decode(js_reply.split(',')[1])
# convert bytes to numpy array
jpg_as_np = np.frombuffer(image_bytes, dtype=np.uint8)
# decode numpy array into OpenCV BGR image
img = cv2.imdecode(jpg_as_np, flags=1)

return img

# function to convert OpenCV Rectangle bounding box image into base64 byte
string to be overlayed on video stream
def bbox_to_bytes(bbox_array):
"""
Params:
bbox_array: Numpy array (pixels) containing rectangle to overlay on
video stream.
Returns:
bytes: Base64 image byte string
"""
# convert array into PIL image
bbox_PIL = PIL.Image.fromarray(bbox_array, 'RGBA')
iobuf = io.BytesIO()
# format bbox into png for return
bbox_PIL.save(iobuf, format='png')
# format return string
bbox_bytes = 'data:image/png;base64,
{}'.format((str(b64encode(iobuf.getvalue()), 'utf-8')))

return bbox_bytes

# JavaScript to properly create our live video stream using our webcam as input
def video_stream():
js = Javascript('''
var video;
var div = null;
var stream;
var captureCanvas;
var imgElement;
var labelElement;

var pendingResolve = null;


var shutdown = false;

function removeDom() {
stream.getVideoTracks()[0].stop();
video.remove();
div.remove();
video = null;
div = null;
stream = null;
imgElement = null;
captureCanvas = null;
labelElement = null;
}

function onAnimationFrame() {
if (!shutdown) {
window.requestAnimationFrame(onAnimationFrame);
}
if (pendingResolve) {
var result = "";
if (!shutdown) {
captureCanvas.getContext('2d').drawImage(video, 0, 0, 640, 480);
result = captureCanvas.toDataURL('image/jpeg', 0.8)
}
var lp = pendingResolve;
pendingResolve = null;
lp(result);
}
}

async function createDom() {


if (div !== null) {
return stream;
}

div = document.createElement('div');
div.style.border = '2px solid black';
div.style.padding = '3px';
div.style.width = '100%';
div.style.maxWidth = '600px';
document.body.appendChild(div);

const modelOut = document.createElement('div');


modelOut.innerHTML = "<span>Status:</span>";
labelElement = document.createElement('span');
labelElement.innerText = 'No data';
labelElement.style.fontWeight = 'bold';
modelOut.appendChild(labelElement);
div.appendChild(modelOut);

video = document.createElement('video');
video.style.display = 'block';
video.width = div.clientWidth - 6;
video.setAttribute('playsinline', '');
video.onclick = () => { shutdown = true; };
stream = await navigator.mediaDevices.getUserMedia(
{video: { facingMode: "environment"}});
div.appendChild(video);

imgElement = document.createElement('img');
imgElement.style.position = 'absolute';
imgElement.style.zIndex = 1;
imgElement.onclick = () => { shutdown = true; };
div.appendChild(imgElement);

const instruction = document.createElement('div');


instruction.innerHTML =
'<span style="color: red; font-weight: bold;">' +
'When finished, click here or on the video to stop this demo</span>';
div.appendChild(instruction);
instruction.onclick = () => { shutdown = true; };

video.srcObject = stream;
await video.play();

captureCanvas = document.createElement('canvas');
captureCanvas.width = 640; //video.videoWidth;
captureCanvas.height = 480; //video.videoHeight;
window.requestAnimationFrame(onAnimationFrame);

return stream;
}
async function stream_frame(label, imgData) {
if (shutdown) {
removeDom();
shutdown = false;
return '';
}

var preCreate = Date.now();


stream = await createDom();

var preShow = Date.now();


if (label != "") {
labelElement.innerHTML = label;
}

if (imgData != "") {
var videoRect = video.getClientRects()[0];
imgElement.style.top = videoRect.top + "px";
imgElement.style.left = videoRect.left + "px";
imgElement.style.width = videoRect.width + "px";
imgElement.style.height = videoRect.height + "px";
imgElement.src = imgData;
}

var preCapture = Date.now();


var result = await new Promise(function(resolve, reject) {
pendingResolve = resolve;
});
shutdown = false;

return {'create': preShow - preCreate,


'show': preCapture - preShow,
'capture': Date.now() - preCapture,
'img': result};
}
''')

display(js)

def video_frame(label, bbox):


data = eval_js('stream_frame("{}", "{}")'.format(label, bbox))
return data

from google.colab.patches import cv2_imshow


# start streaming video from webcam
from PIL import Image as Img
import pylab as pl
try:
video_stream()
from IPython.display import display
# label for video
label_html = 'Capturing...'
# initialze bounding box to empty
bbox = ''
img = []
detections=[]
count = 0
while True:
js_reply = video_frame(label_html, bbox)
if not js_reply:
break
# convert JS response to OpenCV Image
frame = js_to_image(js_reply["img"])

# Only perform detection on every fifth frame


detections.append(frame)
# call our detection function on video frame
#frame_with_detections = detect(detection_graph,Img.fromarray(frame))
#cv2_imshow(frame_with_detections)
except KeyboardInterrupt:
print("Exiting..")
# convert frame with detections to OpenCV format for display
# frame_with_detections_cv2 = np.array(frame_with_detections)
# img.append(cv2.cvtColor(frame_with_detections, cv2.COLOR_RGB2BGR))
# height, width, layers = img[1].shape
# out = cv2.VideoWriter('/content/output.mp4', cv2.VideoWriter_fourcc(*'mp4v'),
fps, (width, height))
# for i in range(len(img)):
# out.write(img[i])

# out.release()

print(len(detections))

# print(detections[0].shape)
# print(detections[1].shape)
# print(detections[2].shape)
# print(detections[3].shape)
# print(detections[4].shape)
# print(detections[5].shape)

from PIL import Image as Img

for detection in detections:


frame_with_detections = detect(detection_graph, Img.fromarray(detection))
frame_with_detections_cv2 = np.array(frame_with_detections)
img.append(cv2.cvtColor(frame_with_detections, cv2.COLOR_RGB2BGR))

print(len(img))

print(img[0])

height, width, layers = img[0].shape


out = cv2.VideoWriter('/content/output.mp4', cv2.VideoWriter_fourcc(*'mp4v'),
0.5, (width, height))
for frame in img:
out.write(frame)

out.release()

You might also like