Skip to content

Commit 16c4e9b

Browse files
committed
Update sample code
1 parent 79c1bfb commit 16c4e9b

File tree

2 files changed

+66
-12
lines changed

2 files changed

+66
-12
lines changed

samples/python/inception_v3/inception_v3.py

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import torchvision.transforms as transforms
1515
from PIL import Image
1616
from PIL.Image import fromarray as ImageFromArray
17+
import argparse
1718

1819
from qai_appbuilder import (QNNContext, Runtime, LogLevel, ProfilingLevel, PerfProfile, QNNConfig)
1920

@@ -45,6 +46,12 @@
4546

4647
inceptionV3 = None
4748

49+
def format_float(num, max_zeros=6):
50+
if abs(num) >= 1e-6:
51+
return f"{num:.10f}".rstrip('0').rstrip('.')
52+
else:
53+
return f"{num:.{max_zeros+2}f}".rstrip('0').rstrip('.')
54+
4855
def preprocess_PIL_image(image: Image) -> torch.Tensor:
4956
preprocess = transforms.Compose([
5057
transforms.Resize(IMAGE_SIZE),
@@ -62,9 +69,19 @@ def post_process(probabilities, output):
6269
categories = [s.strip() for s in f.readlines()]
6370
# Show top categories per image
6471
top5_prob, top5_catid = torch.topk(probabilities, 5)
65-
print("Top 5 predictions for image:\n")
72+
73+
result = "Top 5 predictions for image:\n"
74+
print(result)
75+
6676
for i in range(top5_prob.size(0)):
67-
print(categories[top5_catid[i]], top5_prob[i].item())
77+
cat_id = categories[top5_catid[i]]
78+
item_value = top5_prob[i].item()
79+
item_value = format_float(item_value)
80+
result += f'{cat_id} {item_value} \n'
81+
82+
print(categories[top5_catid[i]], item_value)
83+
84+
return result
6885

6986
# InceptionV3 class which inherited from the class QNNContext.
7087
class InceptionV3(QNNContext):
@@ -115,19 +132,33 @@ def Inference(input_image_path):
115132
# show the Top 5 predictions for image
116133
output = torch.from_numpy(output_data)
117134
probabilities = torch.softmax(output, dim=0)
118-
post_process(probabilities, output)
119-
135+
result=post_process(probabilities, output)
136+
137+
return result
120138

121139
def Release():
122140
global inceptionV3
123141

124142
# Release the resources.
125143
del(inceptionV3)
126144

145+
def main(input = None):
146+
147+
if input is None:
148+
input = execution_ws + "\\input.jpg"
149+
150+
Init()
151+
152+
result = Inference(input)
153+
154+
Release()
127155

128-
Init()
156+
return result
129157

130-
Inference(execution_ws + "\\input.jpg")
131158

132-
Release()
159+
if __name__ == "__main__":
160+
parser = argparse.ArgumentParser(description="Process a single image path.")
161+
parser.add_argument('--image', help='Path to the image', default=None)
162+
args = parser.parse_args()
133163

164+
main(args.image)

samples/python/yolov8_det/yolov8_det.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
from torch.nn.functional import interpolate, pad
1818
from torchvision.ops import nms
1919
from typing import List, Tuple, Optional, Union, Callable
20+
import argparse
21+
2022
from qai_appbuilder import (QNNContext, Runtime, LogLevel, ProfilingLevel, PerfProfile, QNNConfig)
2123

2224
####################################################################
@@ -292,7 +294,7 @@ def Init():
292294
# Instance for YoloV8 objects.
293295
yolov8 = YoloV8("yolov8", madel_path)
294296

295-
def Inference(input_image_path, output_image_path):
297+
def Inference(input_image_path, output_image_path, show_image = True):
296298
global image_buffer, nms_iou_threshold, nms_score_threshold
297299

298300
# Read and preprocess the image.
@@ -349,7 +351,9 @@ def Inference(input_image_path, output_image_path):
349351
#save and display the output_image
350352
output_image = Image.fromarray(output_image)
351353
output_image.save(output_image_path)
352-
output_image.show()
354+
355+
if show_image:
356+
output_image.show()
353357

354358
def Release():
355359
global yolov8
@@ -358,9 +362,28 @@ def Release():
358362
del(yolov8)
359363

360364

361-
Init()
365+
def main(input_image_path=None, output_image_path=None, show_image = True):
366+
367+
if input_image_path is None:
368+
input_image_path = execution_ws + "\\input.jpg"
369+
370+
if output_image_path is None:
371+
output_image_path = execution_ws + "\\output.png"
372+
373+
Init()
374+
375+
Inference(input_image_path=input_image_path,output_image_path=output_image_path,show_image=show_image)
376+
377+
Release()
378+
379+
return "Yolo V8 Inference Result"
362380

363-
Inference(execution_ws + "\\input.jpg", execution_ws + "\\output.jpg")
381+
if __name__ == '__main__':
382+
parser = argparse.ArgumentParser(description="Process a single image path.")
383+
parser.add_argument('--input_image_path', help='Path to the input image', default=None)
384+
#input_image_path, output_image_path
385+
parser.add_argument('--output_image_path', help='Path to the output image', default=None)
386+
args = parser.parse_args()
364387

365-
Release()
388+
main(args.input_image_path, args.output_image_path)
366389

0 commit comments

Comments
 (0)