<jupyter_start><jupyter_text>Press this Play button to Set Up the Models<jupyter_code>from google.colab.output import eval_js

!git clone https://github.com/SarthakJaingit/Visually-Impaired-Food-Device-Aid-.git
%cd /content/Visually-Impaired-Food-Device-Aid-
!pip install effdet
!pip install timm

import webcam_backend 
import inference_device
import torch
import numpy as np
from PIL import Image
import io
import base64
import random
import cv2

def take_photo(label, img_data):
  data = eval_js('takePhoto("{}", "{}")'.format(label, img_data))
  return data

device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
model = inference_device.load_torchvision_models("mobilenet_fasterrcnn", device)

def run_this():
    webcam_backend.start_input()
    label_html = 'Capturing...'
    img_data = ''
    count = 0

    color=None
    label=None
    line_thickness=None
    classes = ["Placeholder", "Apples", "Strawberry", "Peach", "Tomato", "Bad_Spots"]

    while True:
        js_reply = take_photo(label_html, img_data)
        if not js_reply:
            break
        
        image = webcam_backend.js_reply_to_image(js_reply)
        prediction = webcam_backend.webcam_infer_image(image, model, 0.15, [0.35, 0.1], device)

        drawing_array = np.zeros([512,512,4], dtype=np.uint8)

        for i, x in enumerate(prediction[0]['boxes']):
          tl = line_thickness or round(0.002 * (drawing_array.shape[0] + drawing_array.shape[1]) / 2) + 1  # line/font thickness
          color = color or [random.randint(0, 255) for _ in range(3)]
          c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))
          cv2.rectangle(drawing_array, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)
          label = classes[prediction[0]["labels"][i]]
          if label:
            tf = max(tl - 1, 1)  # font thickness
            t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]
            c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3
            cv2.rectangle(drawing_array, c1, c2, color, -1, cv2.LINE_AA)  # filled
            cv2.putText(drawing_array, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)

        drawing_array[:,:,3] = (drawing_array.max(axis = 2) > 0 ).astype(int) * 255

        drawing_PIL = Image.fromarray(drawing_array, 'RGBA')
        iobuf = io.BytesIO()
        drawing_PIL.save(iobuf, format='png')
        drawing_bytes = 'data:image/png;base64,{}'.format((str(base64.b64encode(iobuf.getvalue()), 'utf-8')))

        img_data = drawing_bytes<jupyter_output>Cloning into 'Visually-Impaired-Food-Device-Aid-'...
remote: Enumerating objects: 210, done.[K
remote: Counting objects: 100% (210/210), done.[K
remote: Compressing objects: 100% (163/163), done.[K
remote: Total 210 (delta 99), reused 142 (delta 43), pack-reused 0[K
Receiving objects: 100% (210/210), 123.65 MiB | 26.83 MiB/s, done.
Resolving deltas: 100% (99/99), done.
Checking out files: 100% (22/22), done.
/content/Visually-Impaired-Food-Device-Aid-
Collecting effdet
  Downloading effdet-0.2.4-py3-none-any.whl (111 kB)
[K     |████████████████████████████████| 111 kB 5.4 MB/s 
[?25hCollecting omegaconf>=2.0
  Downloading omegaconf-2.1.1-py3-none-any.whl (74 kB)
[K     |████████████████████████████████| 74 kB 2.9 MB/s 
[?25hRequirement already satisfied: torch>=1.4 in /usr/local/lib/python3.7/dist-packages (from effdet) (1.9.0+cu102)
Requirement already satisfied: torchvision in /usr/local/lib/python3.7/dist-packages (from effdet) (0.10.0+cu102)
Requirement already satisfied: [...]<jupyter_text>Run this cell to use webcamNote: For best results do the following: * Hold fruits relatively still close to camera and wait for like 2-3 seconds till the model makes continual predictions * If you move the object around fast expect a noticeable delay in the drawing of boxes on the frame.<jupyter_code>run_this()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15308201732042792, 'fraction_numerical': 0.053234844625573104, 'mean_word_length': 3.7142857142857144, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 11, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '76970', 'license': 'MIT', 'n_tokens_mistral': 1597, 'n_tokens_neox': 1421, 'n_words': 341, 'path': 'Try_This_Detection_Demo.ipynb', 'repo_name': 'SarthakJaingit/Artificially-Intelligent-Food-Assistant-for-the-Visually-Impaired', 'size': '3926'}