Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Gagan824 authored Aug 7, 2021
0 parents commit b6bbad8
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 0 deletions.
165 changes: 165 additions & 0 deletions YOLOV5.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "c17ddb86",
"metadata": {},
"source": [
"# importing libraries"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "45e74618",
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"import cv2"
]
},
{
"cell_type": "markdown",
"id": "0911b20b",
"metadata": {},
"source": [
"# calling YOLOV5s (yolo v5 small) model using torch"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "27689332",
"metadata": {},
"outputs": [],
"source": [
"# Model\n",
"model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # or yolov5m, yolov5l, yolov5x, custom\n"
]
},
{
"cell_type": "markdown",
"id": "638d865d",
"metadata": {},
"source": [
"# reading video from the directory, processing the video and then writing the output video"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3c4ec705",
"metadata": {},
"outputs": [],
"source": [
"#cap = cv2.VideoCapture(0)\n",
"\n",
"# reading video\n",
"cap = cv2.VideoCapture('test1.mp4')\n",
"\n",
"# finding the height and width of the video frames\n",
"frame_width = int(cap.get(3))\n",
"frame_height = int(cap.get(4))\n",
"\n",
"# creating object to write video in AVI extension\n",
"out = cv2.VideoWriter('outpy1.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 10, (frame_width,frame_height))\n",
"\n",
"while(True):\n",
" \n",
" # reading frame from the video\n",
" ret, frame = cap.read() \n",
" \n",
" # passing the read frames to the YOLOV5s model\n",
" results = model(frame)\n",
" \n",
" # finding the x1,y1,x2,y2 coordinates and labels of the bounding boxes of the detected objects in the video from the result object\n",
" cords = results.xyxy[0].numpy()\n",
" \n",
" # reading individual box data\n",
" for c in cords:\n",
" \n",
" # finding coordinates and labels\n",
" # x1 = c[0]\n",
" # x2 = c[2]\n",
" # x3 = c[1]\n",
" # x4 = c[3]\n",
" \n",
" #creating points\n",
" \n",
" # point1\n",
" p1 = (int(c[0]),int(c[1]))\n",
" # point2 \n",
" p2 = (int(c[2]),int(c[3]))\n",
" \n",
" # drawing bounding boxes/rectangles around the objects using found coordinates\n",
" image = cv2.rectangle(frame, p1,p2, (36,255,12), 2)\n",
" \n",
" # putting text (labels) on the rectangle\n",
" cv2.putText(image,results.names[int(c[5])], (int(c[0]), int(c[1])-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (36,255,12), 2,lineType=cv2.LINE_AA)\n",
"\n",
" # here writing the video using the out object that we have created above\n",
" out.write(frame)\n",
" \n",
" if cv2.waitKey(1) & 0xFF == ord('q'):\n",
" break\n",
" \n",
"cap.release()\n",
"cv2.destroyAllWindows()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e3e1de33",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "965abaa8",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "5f058eb5",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "340db310",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Binary file added outpy1_lGGXQzao_AfwP.avi
Binary file not shown.
Binary file added outpy_ObM23lMC_xgis.avi
Binary file not shown.
31 changes: 31 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# pip install -r requirements.txt

# base ----------------------------------------
matplotlib>=3.2.2
numpy>=1.18.5
opencv-python>=4.1.2
Pillow
PyYAML>=5.3.1
scipy>=1.4.1
torch>=1.7.0
torchvision>=0.8.1
tqdm>=4.41.0

# logging -------------------------------------
tensorboard>=2.4.1
# wandb

# plotting ------------------------------------
seaborn>=0.11.0
pandas

# export --------------------------------------
# coremltools>=4.1
# onnx>=1.9.0
# scikit-learn==0.19.2 # for coreml quantization

# extras --------------------------------------
# Cython # for pycocotools https://github.com/cocodataset/cocoapi/issues/172
# pycocotools>=2.0 # COCO mAP
# albumentations>=1.0.3
thop # FLOPs computation
Binary file added test1.mp4
Binary file not shown.

0 comments on commit b6bbad8

Please sign in to comment.