Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How do I see outputs of every layer of DNN OpenCV Python (visualization tool)?

I'd like to see the output of every layer of DNN under OpenCV Python. So, I am asking you here to help me. If anyone knows a blog or something else explain how to show us outputs of layers, please, put its link in an answer.

I need to see the outputs like this, this

How do I see outputs of every layer of DNN OpenCV Python (visualization tool)?

I'd like to see the output of every layer of DNN under OpenCV Python. So, I am asking you here to help me. If anyone knows a blog or something else explain how to show us outputs of layers, please, put its link in an answer.

I need to see the outputs like this, this

Full code:

import sys
ros_path2 = '/usr/local/lib/python2.7/site-packages'
ros_path3 = '/usr/lib/python2.7/dist-packages'
if  ros_path2 and ros_path3 in sys.path:
    sys.path.remove(ros_path2)
    sys.path.remove(ros_path3)

from imutils.video import VideoStream
from imutils.video import FPS
import numpy as np
# import cupy as np
import argparse
import imutils
import time
import cv2


global out

out = cv2.VideoWriter('outpy.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 3, (640, 480))

# load our serialized model from disk
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe('prototxt.txt', 'caffemodel')     #deep neural network ( dnn )
print("[INFO] starting video stream...")
vs = cv2.VideoCapture(0)
fps = FPS().start()
# loop over the frames from the video stream
while True:
    ret , frame = vs.read()                     #add ret,
    (h, w) = frame.shape[:2]
    blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)),
            0.007843, (300, 300), 127.5)
    net.setInput(blob)
    # detections = net.forward()
    detections = np.array(net.forward())
    big_area = 0
    big_center = 320
    detected = 0
    fps.update()
    fps.stop()
    for i in np.arange(0, detections.shape[2]):
        confidence = detections[0, 0, i, 2]
        object_type = int(detections[0, 0, i, 1])
        if object_type == 15 and confidence > 0.2:                   # it was if confidence > args["confidence"]:

            box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
            (startX, startY, endX, endY) = box.astype("int")
            label = "{}: {:.2f}%".format('person', float(confidence) * 100)
            text1 = " ssd_CPU: ({:.2f} FPS,  {:.2f} sec.)".format(fps.fps(), fps.elapsed())
            cv2.rectangle(frame, (startX, startY), (endX, endY),[0,0,255], 2)
            y = startY - 15 if startY - 15 > 15 else startY + 15
            cv2.putText(frame, label, (startX, y),cv2.FONT_HERSHEY_SIMPLEX, 0.5, [0,0,255], 2)
            cv2.putText(frame, text1, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, [0, 0, 255], 3)
            y0 =  startY+ ((endX- startX)/3.2)
            # print 'yo0', y0
            y0 = int(y0)
            rect_area = (endX-startX)*(endY-startY)
            detected = 1
            if rect_area > big_area: # big_area and big_center are used so that the TurtleBot always tracks the closest person
                big_area = rect_area
    out.write(frame)
    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break
    fps.update()
    fps.stop()
    print("[INFO] elapsed time: {:.2f}".format(fps.elapsed()))
    print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
# do a bit of cleanup
vs.release()    
cv2.destroyAllWindows()