32 lines
921 B
Python
32 lines
921 B
Python
|
|
import cv2
|
||
|
|
import numpy as np
|
||
|
|
import sys
|
||
|
|
|
||
|
|
weights = 'models/detection.weights'
|
||
|
|
cfg = 'models/detection.cfg'
|
||
|
|
img = sys.argv[1]
|
||
|
|
|
||
|
|
net = cv2.dnn.readNet(weights, cfg)
|
||
|
|
blob = cv2.dnn.blobFromImage(cv2.imread(img), 0.00392, (416,416), (0,0,0), True, crop=False)
|
||
|
|
net.setInput(blob)
|
||
|
|
|
||
|
|
layer_names = net.getLayerNames()
|
||
|
|
unconnected = net.getUnconnectedOutLayers()
|
||
|
|
indices = []
|
||
|
|
if isinstance(unconnected, np.ndarray):
|
||
|
|
indices = unconnected.flatten()
|
||
|
|
else:
|
||
|
|
try:
|
||
|
|
indices = [u[0] if hasattr(u, '__iter__') else u for u in unconnected]
|
||
|
|
except Exception:
|
||
|
|
indices = list(unconnected)
|
||
|
|
|
||
|
|
output_layers = [layer_names[int(i) - 1] for i in indices]
|
||
|
|
print('output_layers=', output_layers)
|
||
|
|
|
||
|
|
outs = net.forward(output_layers)
|
||
|
|
print('outs len:', len(outs))
|
||
|
|
for i, om in enumerate(outs):
|
||
|
|
print(i, 'rows', om.shape[0], 'cols', om.shape[1])
|
||
|
|
for r in range(min(3, om.shape[0])):
|
||
|
|
print(' row', r, om[r,:10])
|