# **2021/07/15**
[[5 Running the Live Camera Recognition Demo "ImageNet"]](https://github.com/dusty-nv/jetson-inference/blob/master/docs/imagenet-camera-2.md)
[[6 Locating Objects with "DetectNet"]](https://github.com/dusty-nv/jetson-inference/blob/master/docs/detectnet-console-2.md)
[[8 Coding Your Own Object Detection Program]](https://github.com/dusty-nv/jetson-inference/blob/master/docs/detectnet-example-2.md)
###### tags: `藍柏婷`
###### tags: `2021/07/15`
### **==== 編寫您自己的對象檢測程序 Coding Your Own Object Detection Program ====**
(因為鏡頭還沒送到,我先做[[8 Coding Your Own Object Detection Program]](https://github.com/dusty-nv/jetson-inference/blob/master/docs/detectnet-example-2.md))
先打開`gedit Text Editor`,創一個`Editor`,"my-detection_1.py"
**因為原檔有錯誤,所以要改一下!!!**
如下:
```python=
import jetson.inference
import jetson.utils
net = jetson.inference.detectNet("ssd-mobilenet-v2", threshold=0.5)
#camera = jetson.utils.videoSource("csi://0") # csi camera
camera = jetson.utils.videoSource("/dev/video0") # usb camera
display = jetson.utils.videoOutput("display://0") # 'my_video.mp4' for file
while True:
img = camera.Capture()
detections = net.Detect(img)
display.Render(img)
display.SetStatus("Object Detection | Network {:.0f} FPS".format(net.GetNetworkFPS()))
if not camera.IsStreaming() or not display.IsStreaming():
break
```
>```python=4
>net = jetson.inference.detectNet("ssd-mobilenet-v2", threshold=0.5)
>```
>-> 0.5是指50%以上才顯示
>
>```python=6
>camera = jetson.utils.videoSource("/dev/video0") # usb camera
>```
>->這才是我們的鏡頭 (V4L2 camera)
就這樣!!!
執行時,先從`Desktop/scifair/image_classifiction_demo/jetson-inference/build/aarch64/bin`,進入`bin`後,直接輸入
$ python3 my-detection_1.py
若無誤,將會顯示鏡頭,finish!
(我發現還是要鏡頭,呵呵!)
---
### **==== 運行實時攝像機識別演示 Running the Live Camera Recognition Demo ====**
(回歸昨天做的延續,呵呵!)
執行時,先從`Desktop/scifair/image_classifiction_demo/jetson-inference/build/aarch64/bin`,進入`bin`後,直接輸入
$ ./imagenet.py /dev/video0
---
### **==== 使用 DetectNet 定位對象 Locating Objects with DetectNet ====**
執行時,先從`Desktop/scifair/image_classifiction_demo/jetson-inference/build/aarch64/bin`,進入`bin`後,直接輸入
$ ./detectnet.py --network=ssd-mobilenet-v2 images/peds_0.jpg images/test/output.jpg
>-> peds_0.jpg是要改的照片
記得跑完之後要點網址才會顯示結果(ctrl+點網址)
**試著看懂這段 Source code**
```python=
import jetson.inference
import jetson.utils
import argparse
import sys
# parse the command line
parser = argparse.ArgumentParser(description="Locate objects in a live camera stream using an object detection DNN.")
parser.add_argument("input_URI", type=str, default="", nargs='?', help="URI of the input stream")
parser.add_argument("output_URI", type=str, default="", nargs='?', help="URI of the output stream")
parser.add_argument("--network", type=str, default="ssd-mobilenet-v2", help="pre-trained model to load (see below for options)")
parser.add_argument("--overlay", type=str, default="box,labels,conf", help="detection overlay flags (e.g. --overlay=box,labels,conf)\nvalid combinations are: 'box', 'labels', 'conf', 'none'")
parser.add_argument("--threshold", type=float, default=0.5, help="minimum detection threshold to use")
try:
opt = parser.parse_known_args()[0]
except:
print("")
parser.print_help()
sys.exit(0)
# load the object detection network
net = jetson.inference.detectNet(opt.network, sys.argv, opt.threshold)
# create video sources & outputs
input = jetson.utils.videoSource(opt.input_URI, argv=sys.argv)
output = jetson.utils.videoOutput(opt.output_URI, argv=sys.argv)
# process frames until the user exits
while True:
# capture the next image
img = input.Capture()
# detect objects in the image (with overlay)
detections = net.Detect(img, overlay=opt.overlay)
# print the detections
print("detected {:d} objects in image".format(len(detections)))
for detection in detections:
print(detection)
# render the image
output.Render(img)
# update the title bar
output.SetStatus("{:s} | Network {:.0f} FPS".format(opt.network, net.GetNetworkFPS()))
# print out performance info
net.PrintProfilerTimes()
# exit on input/output EOS
if not input.IsStreaming() or not output.IsStreaming():
break
```
>```python=1
>import jetson.inference
>```
>-> = include (C++)
>
>```python=12
>parser.add_argument("--network", type=str, default="ssd-mobilenet-v2", help="pre-trained model to load (see below for options)")
>```
>-> 前有`--`代表可有可無,但是加上去可以有一些功能
>
>```python=13
>parser.add_argument("--overlay", type=str, default="box,labels,conf", help="detection overlay flags (e.g. --overlay=box,labels,conf)\nvalid combinations are: 'box', 'labels', 'conf', 'none'")
>```
>-> `--overlay` 是有"box,labels,conf"的功能
>
>```python=14
>parser.add_argument("--threshold", type=float, default=0.5, help="minimum detection threshold to use")
>```
>-> 可以把`--threshold`想成是過濾器,`default=0.5`是指大於50%再輸出
>
>```python=16
>try:
>```
>-> `try`跟`except`可以想成是一個防呆裝置,確保他可以跑出結果
>
>```python=17
> opt = parser.parse_known_args()[0]
>```
>-> 去上面抓現有的資料
>
>```python=23
># load the object detection network
>net = jetson.inference.detectNet(opt.network, sys.argv, opt.threshold)
>```
>->是函式庫裡的一個工具,一定要有( , , )三格
>
>```python=44
> # render the image
> output.Render(img)
>```
>-> 把"box,labels,conf"跟照片合併起來
>
>```python=50
> # print out performance info
> net.PrintProfilerTimes()
>```
>-> 印出一些跑的時候的資料
>
>```python=53
> # exit on input/output EOS
> if not input.IsStreaming() or not output.IsStreaming():
>```
>-> 如果沒有input,就break