
import numpy as np
import cv2
import sys

if len(sys.argv) != 4:
    print("Usage: dnn.py <prototxt> <caffemodel> <image>")
    sys.exit(1)

cv2.ocl.setUseOpenCL(False)

# read the image
test_img = cv2.imread(sys.argv[3])

# resize
resized = cv2.resize(test_img, (224,224))
converted = np.moveaxis(resized, 2, 0)
data = np.reshape(converted.astype(np.float32), (-1, 3, 224, 224))

# initialize network
net = cv2.dnn.readNetFromCaffe(sys.argv[1], sys.argv[2])
net.setBlob(".data", data)

# run the network
net.forward()

# print the class
print(str(net.getBlob("prob").argmax()))


import numpy as np
import cv2
import sys

if len(sys.argv) != 4:
    print("Usage: dnn.py <prototxt> <caffemodel> <image>")
    sys.exit(1)

cv2.ocl.setUseOpenCL(False)

# read the image
test_img = cv2.imread(sys.argv[3])

# resize
resized = cv2.resize(test_img, (224,224))
converted = np.moveaxis(resized, 2, 0)
data = np.reshape(converted.astype(np.float32), (-1, 3, 224, 224))

# initialize network
net = cv2.dnn.readNetFromCaffe(sys.argv[1], sys.argv[2])
net.setInput(data, "data")

# run the network
result = net.forward("prob")

# print the class
print(str(result.argmax()))

Fix DNN test to be compatible with OpenCV 3.3.
convnet: Fix DNN test to be compatible with OpenCV 3.3.

OpenCV DNN module API changed with OpenCV 3.3. Fix the tests to use the
new API.

Signed-off-by: Ismo Puustinen <75dda586a9213f0e0695eb79120c94222bb30e60@intel.com>

  
  import numpy as np
  import cv2
  import sys
  
  if len(sys.argv) != 4:
      print("Usage: dnn.py <prototxt> <caffemodel> <image>")
      sys.exit(1)
  
  cv2.ocl.setUseOpenCL(False)
  
  # read the image
  test_img = cv2.imread(sys.argv[3])
  
  # resize
  resized = cv2.resize(test_img, (224,224))
  converted = np.moveaxis(resized, 2, 0)
  data = np.reshape(converted.astype(np.float32), (-1, 3, 224, 224))
  
  # initialize network
  net = cv2.dnn.readNetFromCaffe(sys.argv[1], sys.argv[2])
- net.setBlob(".data", data)
+ net.setInput(data, "data")
  
  # run the network
- net.forward()
+ result = net.forward("prob")
  
  # print the class
- print(str(net.getBlob("prob").argmax()))
+ print(str(result.argmax()))
  
Fix DNN test to be compatible with OpenCV 3.3.
## Code Before:

import numpy as np
import cv2
import sys

if len(sys.argv) != 4:
    print("Usage: dnn.py <prototxt> <caffemodel> <image>")
    sys.exit(1)

cv2.ocl.setUseOpenCL(False)

# read the image
test_img = cv2.imread(sys.argv[3])

# resize
resized = cv2.resize(test_img, (224,224))
converted = np.moveaxis(resized, 2, 0)
data = np.reshape(converted.astype(np.float32), (-1, 3, 224, 224))

# initialize network
net = cv2.dnn.readNetFromCaffe(sys.argv[1], sys.argv[2])
net.setBlob(".data", data)

# run the network
net.forward()

# print the class
print(str(net.getBlob("prob").argmax()))

## Instruction:
Fix DNN test to be compatible with OpenCV 3.3.
## Code After:

import numpy as np
import cv2
import sys

if len(sys.argv) != 4:
    print("Usage: dnn.py <prototxt> <caffemodel> <image>")
    sys.exit(1)

cv2.ocl.setUseOpenCL(False)

# read the image
test_img = cv2.imread(sys.argv[3])

# resize
resized = cv2.resize(test_img, (224,224))
converted = np.moveaxis(resized, 2, 0)
data = np.reshape(converted.astype(np.float32), (-1, 3, 224, 224))

# initialize network
net = cv2.dnn.readNetFromCaffe(sys.argv[1], sys.argv[2])
net.setInput(data, "data")

# run the network
result = net.forward("prob")

# print the class
print(str(result.argmax()))
