CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Reposted on 2023-11-22
by mattmy

Original Post

Original - Posted on 2023-11-20
by mattmy



            
Present in both answers; Present only in the new answer; Present only in the old answer;

Realised my mistake was using np.expand_dims instead of cv2.dnn.blobFromImage, here is the working code for anyone with a similar issue:
``` import cv2 from openvino.runtime import Core
ie = Core()
model = ie.read_model(model="best_openvino_model/best.xml") compiled_model = ie.compile_model(model=model, device_name="CPU")
input_layer_ir = compiled_model.input(0) output_layer_ir = compiled_model.output()
image = cv2.imread("../test.jpg") # N, C, H, W = input_layer_ir.shape N, C, H, W = 1, 1, 480, 480 # resized_image = cv2.resize(image, (W, H)) # input_image = np.expand_dims(resized_image.transpose(2, 0, 1), 0) input_image = cv2.dnn.blobFromImage(resized_image, 1/255, (W, H), [0,0,0], 1, crop=False) output = compiled_model([input_image])[output_layer_ir] output = cv2.transpose(output[0])
boxes = [] scores = [] class_ids = []
for row in output: # Each row is [x, y, width, height, probability class 0, probability class 1, ...] classes_scores = row[4:] (_min_score, max_score, _min_class_loc, (_x, max_class_index)) = cv2.minMaxLoc(classes_scores) if max_score >= 0.25: box = [row[0] - (0.5 * row[2]), row[1] - (0.5 * row[3]), row[2], row[3]] boxes.append(box) scores.append(max_score) class_ids.append(max_class_index)
[height, width, _] = image.shape length = max((height, width)) scale = length/480
# Apply NMS (Non-maximum suppression) RESULT_BOXES = cv2.dnn.NMSBoxes(boxes, scores, 0.5, 0.6, 0.5) for index in RESULT_BOXES: box = boxes[index] x, y = round(box[0] * scale), round(box[1] * scale) x_plus_w, y_plus_h = round((box[0] + box[2]) * scale), round((box[1] + box[3]) * scale) image = cv2.rectangle(cv2.UMat(image), (x, y), (x_plus_w, y_plus_h), (0, 0, 255), 8) image = cv2.putText(cv2.UMat(image), f"handgun {round(scores[index], 2)}", (x - 10, y - 12), cv2.FONT_HERSHEY_SIMPLEX, 3, (0,0,255), 8)
makedirs("runs/openvino", exist_ok=True) cv2.imwrite("test_openvino_runtime.jpg", image) ```
A bit late to the party, but I have stumbled upon this problem too, I believe from the output shape [1,6,8400] you are seeing you can take the first value and transpose to [8400, 6] giving you 8400 rows of the format [x, y, width, height, probability class 0, probability class 1], assuming you have 2 classes. So to process the code would look like: ``` import cv2 import matplotlib.pyplot as plt import numpy as np from openvino.runtime import Core
ie = Core()
model = ie.read_model(model=r"\models\IR\model_fit.xml") compiled_model = ie.compile_model(model=model, device_name="CPU")
input_layer_ir = compiled_model.input(0) output_layer_ir = compiled_model.output()
image = cv2.imread(r'\Resources\test_image.jpg') N, C, H, W = input_layer_ir.shape resized_image = cv2.resize(image, (W, H)) input_image = np.expand_dims(resized_image.transpose(2, 0, 1), 0) plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)); output = compiled_model([input_image])[output_layer_ir]
output = cv2.transpose(output[0]) print("Response shape", output.shape)
boxes = [] scores = [] class_ids = []
for row in output: # Each row is [x, y, width, height, probability class 0, probability class 1, ...] classes_scores = row[4:] (_min_score, max_score, _min_class_loc, (_x, max_class_index)) = cv2.minMaxLoc(classes_scores) if max_score >= 0.25: box = [row[0] - (0.5 * row[2]), row[1] - (0.5 * row[3]), row[2], row[3]] boxes.append(box) scores.append(max_score) class_ids.append(max_class_index)
[height, width, _] = image.shape length = max((height, width)) scale = length/480
# Apply NMS (Non-maximum suppression) RESULT_BOXES = cv2.dnn.NMSBoxes(boxes, scores, 0.5, 0.6, 0.5) for index in RESULT_BOXES: box = boxes[index] x, y = round(box[0] * scale), round(box[1] * scale) x_plus_w, y_plus_h = round((box[0] + box[2]) * scale), round((box[1] + box[3]) * scale) draw_image = cv2.rectangle(cv2.UMat(image), (x, y), (x_plus_w, y_plus_h), (0, 0, 255), 4)
cv2.imwrite(r'\Resources\test_image_result.jpg', image) ``` There is more detail here: https://github.com/ultralytics/ultralytics/blob/main/examples/YOLOv8-OpenCV-ONNX-Python/main.py
Hope that helps, and if so could you let me know how you exported your model from YoloV8? I have done it but it gives me weird results using the IE Core instead of loading and running with YOLO

        
Present in both answers; Present only in the new answer; Present only in the old answer;