<jupyter_start><jupyter_text>Detect hand handle phone<jupyter_code>import colorsys
import cv2
import dlib
# import face_recognition
from keras import backend as K
from keras.utils import multi_gpu_model
from keras.layers import Input
from keras.models import load_model

import numpy as np
import os
from PIL import Image, ImageFont, ImageDraw
from timeit import default_timer as timer

from yolo3.model import yolo_eval, yolo_body, tiny_yolo_body
from yolo3.utils import letterbox_image
from imutils.video import WebcamVideoStream
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
gpu_num = 1
model_path = 'logs/iphone_damvantai_anhTuan_rotated_flip/ep066-loss9.892-val_loss9.687_rorated.h5'
anchors_path = 'model_data/yolo_anchors.txt'
classes_path = 'model_data/hand.txt'
score = 0.75
iou = 0.25
model_image_size = (416, 416)
sess = K.get_session()
# Get class
classes_path = os.path.expanduser(classes_path)
with open(classes_path) as f:
    class_names = f.readlines()

class_names = [c.strip() for c in class_names]
anchors_path = os.path.expanduser(anchors_path)
with open(anchors_path) as f:
    anchors = f.readline()
anchors = [float(x) for x in anchors.split(',')]
anchors = np.array(anchors).reshape(-1, 2)
# Load model
model_path = os.path.expanduser(model_path)
assert model_path.endswith('.h5'), 'Keras model end with file .h5'

num_anchors = len(anchors)
num_classes = len(class_names)

is_tiny_version = num_anchors==6
try:
    yolo_model = load_model(model_path, compile=False)
except:
    if is_tiny_version:
        yolo_model = tiny_yolo_body(Input(shape=(None, None, 3)), num_anchors//2, num_classes)
    else:
        yolo_model = yolo_body(Input(shape=(None, None, 3)), num_anchors//3, num_classes)
    
    yolo_model.load_weights(model_path)
else:
    yolo_model.layers[-1].output_shape[-1] == num_anchors/len(yolo_model.output) * (num_classes + 5), 'Mismatch between model and given anchor and class sizes'
    
print("{} model, anchors, and classes loaded.".format(model_path))
input_image_shape = K.placeholder(shape=(2, ))
boxes, scores, classes = yolo_eval(yolo_model.output, anchors, len(class_names), input_image_shape, score_threshold=score, iou_threshold=iou)
height = 480
width = 640
num_frame = 0
font = cv2.FONT_HERSHEY_DUPLEX

video_capture = WebcamVideoStream(src=0).start()

while True:
    num_frame += 1
    frame = video_capture.read()
    frame = cv2.flip(frame, 1)
    
    image = Image.fromarray(frame)
#     image = yolo.detect_image(image)

    # Process detect hand and recognition furniture
    boxed_image = letterbox_image(image, tuple(reversed(model_image_size)))
    image_data = np.array(boxed_image, dtype='float32')
    
    image_data /= 255.
    image_data = np.expand_dims(image_data, 0)
    
    out_boxes, out_scores, out_classes = sess.run([boxes, scores, classes],
                                                 feed_dict={
                                                     yolo_model.input: image_data,
                                                     input_image_shape: [image.size[1], image.size[0]],
                                                     K.learning_phase(): 0
                                                 })
    
    print("Found {} boxes for {}".format(len(out_boxes), 'img'))
    
    for i, c in reversed(list(enumerate(out_classes))):
        predicted_class = class_names[c]
        box = out_boxes[i]
        score = out_scores[i]        
        
        label = '{} {:.2f}'.format(predicted_class, score)
        
        top, left, bottom, right = box
        
        top = max(0, np.floor(top + 0.5).astype('int32'))
        left = max(0, np.floor(left + 0.5).astype('int32'))
        bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
        right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
#         print(label, (left, top), (right, bottom))
        
        label = 'hand with phone: {}'.format(score)
        
        cv2.rectangle(frame, (left, top), (right, bottom), (255, 0, 0), 3)
        cv2.putText(frame, label, (left + 6, top - 6), font, 1.0, (0, 0, 255), 1)
        
    cv2.imshow("Frame", frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break<jupyter_output>Found 0 boxes for img
Found 0 boxes for img
Found 0 boxes for img
Found 0 boxes for img
Found 0 boxes for img
Found 0 boxes for img
Found 0 boxes for img
Found 0 boxes for img
Found 0 boxes for img
Found 0 boxes for img
Found 0 boxes for img
Found 0 boxes for img
Found 0 boxes for img
Found 1 boxes for img
Found 1 boxes for img
Found 1 boxes for img
Found 1 boxes for img
Found 1 boxes for img
Found 1 boxes for img
Found 1 boxes for img
Found 1 boxes for img
Found 1 boxes for img
Found 1 boxes for img
Found 1 boxes for img
Found 1 boxes for img
Found 1 boxes for img
Found 1 boxes for img
Found 1 boxes for img
Found 1 boxes for img
Found 1 boxes for img
Found 1 boxes for img
Found 1 boxes for img
Found 1 boxes for img
Found 1 boxes for img
Found 0 boxes for img
Found 0 boxes for img
Found 0 boxes for img
Found 1 boxes for img
Found 1 boxes for img
Found 0 boxes for img
Found 0 boxes for img
Found 1 boxes for img
Found 0 boxes for img
Found 0 boxes for img
Found 0 boxes for img
Found 0 bo[...]
{'fraction_non_alphanumeric': 0.09820061255742726, 'fraction_numerical': 0.02909647779479326, 'mean_word_length': 3.06298600311042, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '520651', 'license': 'MIT', 'n_tokens_mistral': 1807, 'n_tokens_neox': 1687, 'n_words': 542, 'path': 'src/Webcam_tested_iphone.ipynb', 'repo_name': 'damvantai/pose_human_with_face_recognition', 'size': '5224'}