Ask Your Question
1

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

asked 2020-05-28 20:15:15 -0600

redhwan gravatar image

updated 2020-05-28 20:48:10 -0600

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()
edit retag flag offensive close merge delete

Comments

1

Hinton diagram.

sjhalayka gravatar imagesjhalayka ( 2020-05-28 21:16:46 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-05-29 01:09:30 -0600

berak gravatar image

there is a nice tool here:

https://github.com/Pandinosaurus/Cpp-...

(in c++, unfortunately)

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-05-28 20:15:15 -0600

Seen: 1,092 times

Last updated: May 29 '20