Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14040

TypeError: '_UserObject' object is not callable while running saved mode in Tensorflow

$
0
0

I'm new to Tensorflow and Python, I'm running custom object detection in Google Colab and following this tutorial -

https://github.com/tensorflow/models/blob/master/docs/vision/object_detection.ipynb

and all the steps were running successfully and got saved_model files. Now I'm running the following code to detect the object through webcam (Flask), but I'm getting the following error: Please help me.

Debugging middleware caught exception in streamed response at a point where response headers were already sent.Traceback (most recent call last):  File "C:\ProgramData\Anaconda3\Lib\site-packages\werkzeug\wsgi.py", line 462, in __next__    return self._next()  File "C:\ProgramData\Anaconda3\Lib\site-packages\werkzeug\wrappers\response.py", line 50, in _iter_encoded    for item in iterable:  File "E:\Final project\ran\IP cam\app - Copy (2).py", line 42, in object_detection    detections = detect_fn(input_tensor)TypeError: '_UserObject' object is not callable

Please find my complete code below:

from flask import Flask, render_template, Responseimport tensorflow as tfimport numpy as npimport cv2from object_detection.utils import label_map_utilfrom object_detection.utils import visualization_utils as viz_utilsfrom pathlib import Pathfrom PIL import Image, ImageDrawimport ioimport threadingapp = Flask(__name__)detect_fn = tf.saved_model.load("./saved_model/new")PATH_TO_LABELS = './saved_model/new/label_map.pbtxt'with open(PATH_TO_LABELS, 'rb') as f:    label_map = f.read()category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)def object_detection():    cap = cv2.VideoCapture(0)    while True:        success, frame = cap.read()  # Read frame from webcam        if not success:            print("Could not open video device")            break        # Perform inference        input_tensor = tf.convert_to_tensor(frame)        input_tensor = input_tensor[tf.newaxis, ...]        detections = detect_fn(input_tensor)        # Visualize the results        viz_utils.visualize_boxes_and_labels_on_image_array(            frame,            detections['detection_boxes'][0].numpy(),            detections['detection_classes'][0].numpy().astype(int),            detections['detection_scores'][0].numpy(),            category_index,            use_normalized_coordinates=True,            max_boxes_to_draw=200,            min_score_thresh=0.4,            agnostic_mode=False)        ret, buffer = cv2.imencode('.jpg', frame)        frame = buffer.tobytes()        yield (b'--frame\r\n'               b'Content-Type: image/jpeg\r\n\r\n'+ frame + b'\r\n')    cap.release()@app.route('/')def index():    return render_template('index.html')@app.route('/video_feed')def video_feed():    return Response(object_detection(), mimetype='multipart/x-mixed-replace; boundary=frame')if __name__ == '__main__':    app.run(debug=True)

Viewing all articles
Browse latest Browse all 14040

Trending Articles