資源簡介
運行非常好,由于文件太大,請自己下載yolov3.weights添加到y(tǒng)olo-coco文件夾下面

代碼片段和文件信息
#?import?the?necessary?packages
import?numpy?as?np
import?argparse
import?time
import?cv2
import?os
?
#?construct?the?argument?parse?and?parse?the?arguments
ap?=?argparse.ArgumentParser()
ap.add_argument(“-i“?“--image“?required=True
????help=“path?to?input?image“)
ap.add_argument(“-y“?“--yolo“?required=True
????help=“base?path?to?YOLO?directory“)
ap.add_argument(“-c“?“--confidence“?type=float?default=0.5
????help=“minimum?probability?to?filter?weak?detections“)
ap.add_argument(“-t“?“--threshold“?type=float?default=0.3
????help=“threshold?when?applying?non-maxima?suppression“)
args?=?vars(ap.parse_args())
#?load?the?COCO?class?labels?our?YOLO?model?was?trained?on
labelsPath?=?os.path.sep.join([args[“yolo“]?“coco.names“])
LABELS?=?open(labelsPath).read().strip().split(“\n“)
?
#?initialize?a?list?of?colors?to?represent?each?possible?class?label
np.random.seed(42)
COLORS?=?np.random.randint(0?255?size=(len(LABELS)?3)
????dtype=“uint8“)
#?derive?the?paths?to?the?YOLO?weights?and?model?configuration
weightsPath?=?os.path.sep.join([args[“yolo“]?“yolov3.weights“])
configPath?=?os.path.sep.join([args[“yolo“]?“yolov3.cfg“])
?
#?load?our?YOLO?object?detector?trained?on?COCO?dataset?(80?classes)
print(“[INFO]?loading?YOLO?from?disk...“)
net?=?cv2.dnn.readNetFromDarknet(configPath?weightsPath)
#?load?our?input?image?and?grab?its?spatial?dimensions
image?=?cv2.imread(args[“image“])
(H?W)?=?image.shape[:2]
?
#?determine?only?the?*output*?layer?names?that?we?need?from?YOLO
ln?=?net.getlayerNames()
ln?=?[ln[i[0]?-?1]?for?i?in?net.getUnconnectedOutlayers()]
?
#?construct?a?blob?from?the?input?image?and?then?perform?a?forward
#?pass?of?the?YOLO?object?detector?giving?us?our?bounding?boxes?and
#?associated?probabilities
blob?=?cv2.dnn.blobFromImage(image?1?/?255.0?(416?416)
????swapRB=True?crop=False)
net.setInput(blob)
start?=?time.time()
layerOutputs?=?net.forward(ln)
end?=?time.time()
?
#?show?timing?information?on?YOLO
print(“[INFO]?YOLO?took?{:.6f}?seconds“.format(end?-?start))
#?initialize?our?lists?of?detected?bounding?boxes?confidences?and
#?class?IDs?respectively
boxes?=?[]
confidences?=?[]
classIDs?=?[]
#?loop?over?each?of?the?layer?outputs
for?output?in?layerOutputs:
????#?loop?over?each?of?the?detections
????for?detection?in?output:
????????#?extract?the?class?ID?and?confidence?(i.e.?probability)?of
????????#?the?current?object?detection
????????scores?=?detection[5:]
????????classID?=?np.argmax(scores)
????????confidence?=?scores[classID]
?
????????#?filter?out?weak?predictions?by?ensuring?the?detected
????????#?probability?is?greater?than?the?minimum?probability
????????if?confidence?>?args[“confidence“]:
????????????#?scale?the?bounding?box?coordinates?back?relative?to?the
????????????#?size?of?the?image?keeping?in?mind?that?YOLO?actually
????????????#?returns?the?center?(x?y)-coordinates?of?the?bounding
????????????#?box?followed?by?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????138??2019-01-04?20:03??物體識別成功案例\.idea\encodings.xm
?????文件????????294??2019-01-04?20:03??物體識別成功案例\.idea\misc.xm
?????文件????????307??2019-01-04?20:03??物體識別成功案例\.idea\modules.xm
?????文件????????239??2019-01-04?20:05??物體識別成功案例\.idea\other.xm
?????文件???????7044??2019-01-04?20:28??物體識別成功案例\.idea\workspace.xm
?????文件????????534??2019-01-04?20:04??物體識別成功案例\.idea\物體識別成功案例.iml
?????文件??????54989??2019-01-04?19:57??物體識別成功案例\images\bird.jpg
?????文件??????51596??2019-01-04?20:01??物體識別成功案例\images\people1.jpg
?????文件?????940204??2019-01-04?20:16??物體識別成功案例\output\1.PNG
?????文件?????894003??2019-01-05?12:49??物體識別成功案例\output\2.PNG
?????文件????????705??2019-01-04?16:33??物體識別成功案例\yolo-coco\coco.names
?????文件???????9131??2019-01-04?16:33??物體識別成功案例\yolo-coco\yolov3.cfg
?????文件???????4499??2019-01-04?20:13??物體識別成功案例\yolo.py
?????文件????????284??2019-01-06?17:13??物體識別成功案例\說明.txt
?????目錄??????????0??2019-01-04?20:28??物體識別成功案例\.idea
?????目錄??????????0??2019-01-04?20:14??物體識別成功案例\images
?????目錄??????????0??2019-01-05?12:49??物體識別成功案例\output
?????目錄??????????0??2019-01-04?19:51??物體識別成功案例\yolo-coco
?????目錄??????????0??2019-01-04?20:30??物體識別成功案例
-----------?---------??----------?-----??----
??????????????1963967????????????????????19
評論
共有 條評論