Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
Jetson-Triton-LPR
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YONG-LIN SU
Jetson-Triton-LPR
Commits
cbc73367
Commit
cbc73367
authored
Jun 13, 2022
by
Bruce
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Save Stream per hours by random range in 5 min
parent
0f93fd8f
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
878 additions
and
48 deletions
+878
-48
test_camera-checkpoint.ipynb
.ipynb_checkpoints/test_camera-checkpoint.ipynb
+372
-26
Camera_Jetson.py
VideoAnalysis/Camera_Jetson.py
+2
-2
VideoSplitWriter.py
VideoAnalysis/VideoSplitWriter.py
+132
-0
Camera_Jetson.cpython-36.pyc
VideoAnalysis/__pycache__/Camera_Jetson.cpython-36.pyc
+0
-0
test_camera.ipynb
test_camera.ipynb
+372
-20
No files found.
.ipynb_checkpoints/test_camera-checkpoint.ipynb
View file @
cbc73367
...
...
@@ -2,35 +2,17 @@
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "6ee48419",
"metadata": {},
"execution_count": 1,
"id": "e604eb6d",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"h265\n",
"Using GSTREAMER\n",
"rtspsrc location=rtsp://192.168.5.218/txg/01 ! rtph265depay ! h265parse ! nvv4l2decoder ! nvvidconv ! video/x-raw,format=BGRx ! videoconvert ! video/x-raw,format=BGR ! appsink\n"
]
},
{
"ename": "NameError",
"evalue": "name 'displayMeta' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-2-05f9565df53a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 23\u001b[0;31m \u001b[0;32mraise\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 24\u001b[0m \u001b[0mcam\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 25\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mcam\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-2-05f9565df53a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\u001b[0;31m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mimshow\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"1\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mdisplayMeta\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mframe\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 18\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwaitKey\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m==\u001b[0m\u001b[0mord\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'q'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'displayMeta' is not defined"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Not Using GSTREAMER\n",
"camera stop\n"
]
}
...
...
@@ -43,7 +25,7 @@
"\n",
"source=\"rtsp://192.168.5.218/txg/01\"\n",
"\n",
"cam=Camera(1,source,encoder = \"h265\",width=1920,height=1080)\n",
"cam=Camera(1,source,encoder = \"h265\",width=1920,height=1080
,use_gstr=False
)\n",
"\n",
"cam.start()\n",
"break_flag=False\n",
...
...
@@ -74,10 +56,374 @@
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "9049b3e9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"datetime.datetime(2022, 6, 10, 17, 43, 58, 624123)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import datetime\n",
"datetime.datetime.now()"
]
},
{
"cell_type": "code",
"execution_count": 72,
"id": "0a404d25",
"metadata": {},
"outputs": [],
"source": [
"import datetime\n",
"import os\n",
"import numpy as np\n",
"import sys\n",
"import time\n",
"class VideoSplitWriter(threading.Thread):\n",
" def __init__(self):\n",
" threading.Thread.__init__(self)\n",
" self.lastRunTime = None\n",
" self.lastWritingTime = None\n",
" self.is_Saving = False\n",
" self.is_Running = False\n",
" self.fourcc= cv2.VideoWriter_fourcc('m','p','4','v')\n",
" self.writer = None # cv2.VideoWriter('datetime',fourcc,30.0,(1920,1080))\n",
" self.SaveRoot= \"../../Videos/save\"\n",
" self.Frame = None\n",
" \n",
" def run(self):\n",
" self.is_Running = True\n",
" self.lastRunTime = datetime.datetime.now() - datetime.timedelta(hours = 1)\n",
" while self.is_alive() and self.is_Running:\n",
" if datetime.datetime.now() - self.lastRunTime >= datetime.timedelta(hours = 1):\n",
" self.SetlastRunTime()\n",
" if not self.is_Saving:\n",
" dd = datetime.datetime.now()\n",
" Write_minute = np.random.randint(dd.minute+1,60)\n",
" Write_second = np.random.randint(dd.second,60)\n",
" wait_flag = True\n",
" while wait_flag:\n",
" dd = datetime.datetime.now()\n",
" if dd.minute >= Write_minute and dd.second >= Write_second:\n",
" wait_flag = False\n",
" sys.stdout.write(f\"\\rwait for writing until {str(self.lastRunTime.hour).zfill(2)}:{str(Write_minute).zfill(2)}:{str(Write_second).zfill(2)}\")\n",
" time.sleep(1)\n",
" \n",
" self.StarWriting()\n",
" print (f\"StarWriting at {self.lastWritingTime}\")\n",
" symbo=['/','-','\\\\']\n",
" while datetime.datetime.now() - self.lastWritingTime < datetime.timedelta(minute = 5):\n",
" count = 0\n",
" while self.Frame is None:\n",
" sys.stdout.write(f\"\\rwait for new frame...{symbo[count%3]}\")\n",
" count+=1\n",
" self.writer.write(self.Frame)\n",
" cv2.waitKey(33)\n",
" self.Frame = None\n",
" self.StopWriting()\n",
" print (f\"StopWriting at {datetime.datetime.now()}\")\n",
" else:\n",
" time.sleep(1000*60*60)\n",
" \n",
" \n",
" def SetlastRunTime(self):\n",
" self.lastRunTime = datetime.datetime.now() \n",
" \n",
" \n",
" def StarWriting(self):\n",
" self.is_Saving = True\n",
" self.lastWritingTime = datetime.datetime.now()\n",
" dd = self.lastWritingTime\n",
" video_folder = os.path.join(self.SaveRoot,f\"{str(dd.month).zfill(2)}/{str(dd.day).zfill(2)}/{str(dd.hour).zfill(2)}\")\n",
" if not os.path.exists(video_folder):\n",
" os.makedirs(video_folder)\n",
" video_path = os.path.join(video_folder,f\"{str(dd.minute).zfill(2)}-{str(dd.second).zfill(2)}.mp4\")\n",
" self.writer = cv2.VideoWriter(video_path,self.fourcc,30.0,(1920,1080))\n",
" \n",
" def StopWriting(self):\n",
" self.is_Saving = False\n",
" if self.writer:\n",
" self.writer.release()\n",
" print(\"Saved before Released\")\n",
" self.writer = None\n",
" \n",
" \n",
" def __del__(self):\n",
" if self.writer:\n",
" self.writer.release()\n",
" self.is_Running = False\n",
" self.join()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f5590c70",
"metadata": {},
"outputs": [],
"source": [
"# video writer per hour\n",
"import cv2\n",
"import datetime\n",
"import threading\n",
"import os\n",
"source=\"rtsp://192.168.5.218/txg/01\"\n",
"\n",
"cap=cv2.VideoCapture(source)\n",
"# VioWri = VideoSplitWriter()\n",
"# VioWri.start()\n",
"\n",
"\n",
"dd = datetime.datetime.now()\n",
"SaveRoot = \"../../Videos/save\"\n",
"video_folder = os.path.join(SaveRoot,f\"{str(dd.month).zfill(2)}/{str(dd.day).zfill(2)}/{str(dd.hour).zfill(2)}\")\n",
"if not os.path.exists(video_folder):\n",
" os.makedirs(video_folder)\n",
"video_path = os.path.join(video_folder,f\"{str(dd.minute).zfill(2)}-{str(dd.second).zfill(2)}.mp4\")\n",
"cv2.VideoWriter_fourcc('m','p','4','v')\n",
"fourcc= cv2.VideoWriter_fourcc('m','p','4','v')\n",
"writer = cv2.VideoWriter(video_path,fourcc,30.0,(1920,1080))\n",
"\n",
"break_flag=False\n",
"try:\n",
" cv2.namedWindow(\"1\",cv2.WINDOW_NORMAL)\n",
" cv2.resizeWindow(\"1\",1280,720)\n",
" while True:\n",
" if break_flag:\n",
" break\n",
" ret,frame=cap.read()\n",
" if ret:\n",
" #VioWri.Frame = frame\n",
" writer.write(frame)\n",
" cv2.imshow(\"1\",frame)\n",
" key=cv2.waitKey(33)\n",
" if key==ord('q'):\n",
" break_flag=True\n",
" break\n",
"except Exception as e:\n",
" raise(e)\n",
" cap.release()\n",
" #del(VioWri)\n",
" writer.release()\n",
" cv2.destroyAllWindows()\n",
" \n",
"finally:\n",
" cap.release()\n",
" writer.release()\n",
" #del(VioWri)\n",
" cv2.destroyAllWindows()\n",
" \n",
" \n",
"\n",
" \n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "51eea6d5",
"metadata": {},
"outputs": [],
"source": [
"import datetime \n",
"dd2=datetime.datetime.now()\n"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "61bc7393",
"metadata": {},
"outputs": [],
"source": [
"dd=datetime.datetime.now()"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "60e658ae",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"video_folder = os.path.join(\"qqq\",f\"{str(dd.month).zfill(2)}/{str(dd.day).zfill(2)}/{str(dd.hour).zfill(2)}\")"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "31d7bc78",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'qqq/06/13/09'"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"video_folder"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "ffe9b4e4",
"metadata": {
"scrolled": true
},
"outputs": [
{
"ename": "TypeError",
"evalue": "unsupported operand type(s) for +: 'datetime.datetime' and 'module'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-29-d4d666287864>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdd\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mdatetime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimedelta\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhours\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0mdd2\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mdatetime\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'datetime.datetime' and 'module'"
]
}
],
"source": [
"dd+datetime.timedelta(hours = 1) < dd2+datetime"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "abd5fbdf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2022-06-13 09:35:10.197164\n"
]
}
],
"source": [
"print(dd)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "0466f22d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(dd+datetime.timedelta(hours = 1) - dd) >= datetime.timedelta(hours = 1)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "db826501",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"datetime.datetime(2022, 6, 13, 9, 35, 17, 913109)"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dd2"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "7fe4d62a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"datetime.datetime(2022, 6, 14, 9, 35, 17, 913109)"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dd2+datetime.timedelta( 1)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "7adf4afd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"0\n",
"0\n",
"1\n",
"1\n",
"1\n",
"0\n",
"0\n",
"0\n"
]
}
],
"source": [
"import numpy as np\n",
"for i in range (10):\n",
" print(np.random.randint(0,2))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "
9e7f2e4a
",
"id": "
85f60b71
",
"metadata": {},
"outputs": [],
"source": []
...
...
VideoAnalysis/Camera_Jetson.py
View file @
cbc73367
...
...
@@ -4,7 +4,7 @@ from queue import Queue
import
time
,
numpy
as
np
,
cv2
class
Camera
(
object
):
def
__init__
(
self
,
_id
,
video_path
,
encoder
=
None
,
width
=
1280
,
height
=
720
,
framerate
=
30
,
log
=
None
,
maxsize
=
10
):
def
__init__
(
self
,
_id
,
video_path
,
encoder
=
None
,
width
=
1280
,
height
=
720
,
framerate
=
30
,
log
=
None
,
maxsize
=
10
,
use_gstr
=
True
):
self
.
ID
=
_id
self
.
__log
=
self
.
__log
if
log
is
None
else
log
self
.
__isCaptured
=
False
...
...
@@ -15,7 +15,7 @@ class Camera(object):
self
.
height
=
height
self
.
framerate
=
framerate
self
.
encoder
=
encoder
self
.
use_gstr
=
False
self
.
use_gstr
=
use_gstr
self
.
__thread
=
Thread
(
target
=
self
.
__job
)
self
.
resultQueue
=
Queue
(
maxsize
=
maxsize
)
def
start
(
self
):
...
...
VideoAnalysis/VideoSplitWriter.py
0 → 100644
View file @
cbc73367
import
datetime
import
os
import
numpy
as
np
import
sys
import
time
import
threading
class
VideoSplitWriter
(
threading
.
Thread
):
"""VideoSplitWriter
Saving Video Stream as mp4 per hours. With random 5 minute range.
"""
def
__init__
(
self
):
threading
.
Thread
.
__init__
(
self
)
self
.
lastRunTime
=
None
self
.
lastWritingTime
=
None
self
.
is_Saving
=
False
self
.
is_Running
=
False
self
.
fourcc
=
cv2
.
VideoWriter_fourcc
(
'm'
,
'p'
,
'4'
,
'v'
)
self
.
writer
=
None
# cv2.VideoWriter('datetime',fourcc,30.0,(1920,1080))
self
.
SaveRoot
=
os
.
path
.
join
(
os
.
path
.
expanduser
(
"~"
),
"Videos"
,
"save"
)
self
.
Frame
=
None
def
run
(
self
):
self
.
is_Running
=
True
self
.
lastRunTime
=
datetime
.
datetime
.
now
()
-
datetime
.
timedelta
(
hours
=
1
)
while
self
.
is_alive
()
and
self
.
is_Running
:
if
datetime
.
datetime
.
now
()
-
self
.
lastRunTime
>=
datetime
.
timedelta
(
hours
=
1
):
self
.
SetlastRunTime
()
if
not
self
.
is_Saving
:
dd
=
datetime
.
datetime
.
now
()
Write_minute
=
np
.
random
.
randint
(
dd
.
minute
,
60
)
Write_second
=
np
.
random
.
randint
(
dd
.
second
,
60
)
wait_flag
=
True
symbo
=
[
'/'
,
'-'
,
'
\\
'
]
count
=
0
while
wait_flag
:
dd
=
datetime
.
datetime
.
now
()
if
dd
.
minute
>=
Write_minute
and
dd
.
second
>=
Write_second
:
wait_flag
=
False
sys
.
stdout
.
write
(
f
"
\r
wait for writing until {str(self.lastRunTime.hour).zfill(2)}:{str(Write_minute).zfill(2)}:{str(Write_second).zfill(2)}...{symbo[count
%3
]}"
)
count
+=
1
time
.
sleep
(
1
)
self
.
StarWriting
()
print
(
f
"StarWriting at {self.lastWritingTime}"
)
while
datetime
.
datetime
.
now
()
-
self
.
lastWritingTime
<
datetime
.
timedelta
(
minute
=
5
):
count
=
0
while
self
.
Frame
is
None
:
sys
.
stdout
.
write
(
f
"
\r
wait for new frame...{symbo[count
%3
]}"
)
count
+=
1
self
.
writer
.
write
(
self
.
Frame
)
cv2
.
waitKey
(
33
)
self
.
Frame
=
None
self
.
StopWriting
()
print
(
f
"StopWriting at {datetime.datetime.now()}"
)
else
:
time
.
sleep
(
1000
*
60
*
60
)
def
SetlastRunTime
(
self
):
self
.
lastRunTime
=
datetime
.
datetime
.
now
()
def
StarWriting
(
self
):
self
.
is_Saving
=
True
self
.
lastWritingTime
=
datetime
.
datetime
.
now
()
dd
=
self
.
lastWritingTime
video_folder
=
os
.
path
.
join
(
self
.
SaveRoot
,
f
"{str(dd.month).zfill(2)}/{str(dd.day).zfill(2)}/{str(dd.hour).zfill(2)}"
)
if
not
os
.
path
.
exists
(
video_folder
):
os
.
makedirs
(
video_folder
)
video_path
=
os
.
path
.
join
(
video_folder
,
f
"{str(dd.minute).zfill(2)}-{str(dd.second).zfill(2)}.mp4"
)
self
.
writer
=
cv2
.
VideoWriter
(
video_path
,
self
.
fourcc
,
30.0
,(
1920
,
1080
))
def
StopWriting
(
self
):
self
.
is_Saving
=
False
if
self
.
writer
:
self
.
writer
.
release
()
print
(
"Saved before Released"
)
self
.
writer
=
None
def
__del__
(
self
):
if
self
.
writer
:
self
.
writer
.
release
()
self
.
is_Running
=
False
self
.
join
()
if
__name__
==
'__main__'
:
# video writer per hour
import
cv2
import
datetime
source
=
"rtsp://192.168.5.218/txg/01"
is_display
=
False
cap
=
cv2
.
VideoCapture
(
source
)
VioWri
=
VideoSplitWriter
()
VioWri
.
start
()
break_flag
=
False
try
:
if
is_display
:
cv2
.
namedWindow
(
"1"
,
cv2
.
WINDOW_NORMAL
)
cv2
.
resizeWindow
(
"1"
,
1280
,
720
)
while
True
:
if
break_flag
:
break
ret
,
frame
=
cap
.
read
()
if
ret
:
VioWri
.
Frame
=
frame
if
is_display
:
cv2
.
imshow
(
"1"
,
frame
)
key
=
cv2
.
waitKey
(
33
)
if
key
==
ord
(
'q'
):
break_flag
=
True
break
except
Exception
as
e
:
raise
(
e
)
cap
.
release
()
del
(
VioWri
)
cv2
.
destroyAllWindows
()
finally
:
cap
.
release
()
del
(
VioWri
)
cv2
.
destroyAllWindows
()
VideoAnalysis/__pycache__/Camera_Jetson.cpython-36.pyc
View file @
cbc73367
No preview for this file type
test_camera.ipynb
View file @
cbc73367
...
...
@@ -2,30 +2,18 @@
"cells": [
{
"cell_type": "code",
"execution_count":
3
,
"execution_count":
1
,
"id": "e604eb6d",
"metadata": {},
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"h265\n",
"Using GSTREAMER\n",
"rtspsrc location=rtsp://192.168.5.218/txg/01 ! rtph265depay ! h265parse ! nvv4l2decoder ! nvvidconv ! video/x-raw,format=BGRx ! videoconvert ! video/x-raw,format=BGR ! appsink\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-3-c9f228e1d44d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mbreak_flag\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m \u001b[0mret\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mframe\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcam\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresultQueue\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 17\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mret\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mimshow\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"1\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mframe\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/lib/python3.6/queue.py\u001b[0m in \u001b[0;36mget\u001b[0;34m(self, block, timeout)\u001b[0m\n\u001b[1;32m 162\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mtimeout\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 163\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_qsize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 164\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnot_empty\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwait\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 165\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mtimeout\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 166\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"'timeout' must be a non-negative number\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/lib/python3.6/threading.py\u001b[0m in \u001b[0;36mwait\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m 293\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m# restore state no matter what (e.g., KeyboardInterrupt)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 294\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mtimeout\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 295\u001b[0;31m \u001b[0mwaiter\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0macquire\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 296\u001b[0m \u001b[0mgotit\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 297\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
"Not Using GSTREAMER\n",
"camera stop\n"
]
}
],
...
...
@@ -37,7 +25,7 @@
"\n",
"source=\"rtsp://192.168.5.218/txg/01\"\n",
"\n",
"cam=Camera(1,source,encoder = \"h265\",width=1920,height=1080)\n",
"cam=Camera(1,source,encoder = \"h265\",width=1920,height=1080
,use_gstr=False
)\n",
"\n",
"cam.start()\n",
"break_flag=False\n",
...
...
@@ -70,10 +58,374 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "9049b3e9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"datetime.datetime(2022, 6, 10, 17, 43, 58, 624123)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import datetime\n",
"datetime.datetime.now()"
]
},
{
"cell_type": "code",
"execution_count": 72,
"id": "0a404d25",
"metadata": {},
"outputs": [],
"source": [
"import datetime\n",
"import os\n",
"import numpy as np\n",
"import sys\n",
"import time\n",
"class VideoSplitWriter(threading.Thread):\n",
" def __init__(self):\n",
" threading.Thread.__init__(self)\n",
" self.lastRunTime = None\n",
" self.lastWritingTime = None\n",
" self.is_Saving = False\n",
" self.is_Running = False\n",
" self.fourcc= cv2.VideoWriter_fourcc('m','p','4','v')\n",
" self.writer = None # cv2.VideoWriter('datetime',fourcc,30.0,(1920,1080))\n",
" self.SaveRoot= \"../../Videos/save\"\n",
" self.Frame = None\n",
" \n",
" def run(self):\n",
" self.is_Running = True\n",
" self.lastRunTime = datetime.datetime.now() - datetime.timedelta(hours = 1)\n",
" while self.is_alive() and self.is_Running:\n",
" if datetime.datetime.now() - self.lastRunTime >= datetime.timedelta(hours = 1):\n",
" self.SetlastRunTime()\n",
" if not self.is_Saving:\n",
" dd = datetime.datetime.now()\n",
" Write_minute = np.random.randint(dd.minute+1,60)\n",
" Write_second = np.random.randint(dd.second,60)\n",
" wait_flag = True\n",
" while wait_flag:\n",
" dd = datetime.datetime.now()\n",
" if dd.minute >= Write_minute and dd.second >= Write_second:\n",
" wait_flag = False\n",
" sys.stdout.write(f\"\\rwait for writing until {str(self.lastRunTime.hour).zfill(2)}:{str(Write_minute).zfill(2)}:{str(Write_second).zfill(2)}\")\n",
" time.sleep(1)\n",
" \n",
" self.StarWriting()\n",
" print (f\"StarWriting at {self.lastWritingTime}\")\n",
" symbo=['/','-','\\\\']\n",
" while datetime.datetime.now() - self.lastWritingTime < datetime.timedelta(minute = 5):\n",
" count = 0\n",
" while self.Frame is None:\n",
" sys.stdout.write(f\"\\rwait for new frame...{symbo[count%3]}\")\n",
" count+=1\n",
" self.writer.write(self.Frame)\n",
" cv2.waitKey(33)\n",
" self.Frame = None\n",
" self.StopWriting()\n",
" print (f\"StopWriting at {datetime.datetime.now()}\")\n",
" else:\n",
" time.sleep(1000*60*60)\n",
" \n",
" \n",
" def SetlastRunTime(self):\n",
" self.lastRunTime = datetime.datetime.now() \n",
" \n",
" \n",
" def StarWriting(self):\n",
" self.is_Saving = True\n",
" self.lastWritingTime = datetime.datetime.now()\n",
" dd = self.lastWritingTime\n",
" video_folder = os.path.join(self.SaveRoot,f\"{str(dd.month).zfill(2)}/{str(dd.day).zfill(2)}/{str(dd.hour).zfill(2)}\")\n",
" if not os.path.exists(video_folder):\n",
" os.makedirs(video_folder)\n",
" video_path = os.path.join(video_folder,f\"{str(dd.minute).zfill(2)}-{str(dd.second).zfill(2)}.mp4\")\n",
" self.writer = cv2.VideoWriter(video_path,self.fourcc,30.0,(1920,1080))\n",
" \n",
" def StopWriting(self):\n",
" self.is_Saving = False\n",
" if self.writer:\n",
" self.writer.release()\n",
" print(\"Saved before Released\")\n",
" self.writer = None\n",
" \n",
" \n",
" def __del__(self):\n",
" if self.writer:\n",
" self.writer.release()\n",
" self.is_Running = False\n",
" self.join()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f5590c70",
"metadata": {},
"outputs": [],
"source": [
"# video writer per hour\n",
"import cv2\n",
"import datetime\n",
"import threading\n",
"import os\n",
"source=\"rtsp://192.168.5.218/txg/01\"\n",
"\n",
"cap=cv2.VideoCapture(source)\n",
"# VioWri = VideoSplitWriter()\n",
"# VioWri.start()\n",
"\n",
"\n",
"dd = datetime.datetime.now()\n",
"SaveRoot = \"../../Videos/save\"\n",
"video_folder = os.path.join(SaveRoot,f\"{str(dd.month).zfill(2)}/{str(dd.day).zfill(2)}/{str(dd.hour).zfill(2)}\")\n",
"if not os.path.exists(video_folder):\n",
" os.makedirs(video_folder)\n",
"video_path = os.path.join(video_folder,f\"{str(dd.minute).zfill(2)}-{str(dd.second).zfill(2)}.mp4\")\n",
"cv2.VideoWriter_fourcc('m','p','4','v')\n",
"fourcc= cv2.VideoWriter_fourcc('m','p','4','v')\n",
"writer = cv2.VideoWriter(video_path,fourcc,30.0,(1920,1080))\n",
"\n",
"break_flag=False\n",
"try:\n",
" cv2.namedWindow(\"1\",cv2.WINDOW_NORMAL)\n",
" cv2.resizeWindow(\"1\",1280,720)\n",
" while True:\n",
" if break_flag:\n",
" break\n",
" ret,frame=cap.read()\n",
" if ret:\n",
" #VioWri.Frame = frame\n",
" writer.write(frame)\n",
" cv2.imshow(\"1\",frame)\n",
" key=cv2.waitKey(33)\n",
" if key==ord('q'):\n",
" break_flag=True\n",
" break\n",
"except Exception as e:\n",
" raise(e)\n",
" cap.release()\n",
" #del(VioWri)\n",
" writer.release()\n",
" cv2.destroyAllWindows()\n",
" \n",
"finally:\n",
" cap.release()\n",
" writer.release()\n",
" #del(VioWri)\n",
" cv2.destroyAllWindows()\n",
" \n",
" \n",
"\n",
" \n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "51eea6d5",
"metadata": {},
"outputs": [],
"source": [
"import datetime \n",
"dd2=datetime.datetime.now()\n"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "61bc7393",
"metadata": {},
"outputs": [],
"source": [
"dd=datetime.datetime.now()"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "60e658ae",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"video_folder = os.path.join(\"qqq\",f\"{str(dd.month).zfill(2)}/{str(dd.day).zfill(2)}/{str(dd.hour).zfill(2)}\")"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "31d7bc78",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'qqq/06/13/09'"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"video_folder"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "ffe9b4e4",
"metadata": {
"scrolled": true
},
"outputs": [
{
"ename": "TypeError",
"evalue": "unsupported operand type(s) for +: 'datetime.datetime' and 'module'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-29-d4d666287864>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdd\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mdatetime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimedelta\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhours\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0mdd2\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mdatetime\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'datetime.datetime' and 'module'"
]
}
],
"source": [
"dd+datetime.timedelta(hours = 1) < dd2+datetime"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "abd5fbdf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2022-06-13 09:35:10.197164\n"
]
}
],
"source": [
"print(dd)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "0466f22d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(dd+datetime.timedelta(hours = 1) - dd) >= datetime.timedelta(hours = 1)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "db826501",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"datetime.datetime(2022, 6, 13, 9, 35, 17, 913109)"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dd2"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "7fe4d62a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"datetime.datetime(2022, 6, 14, 9, 35, 17, 913109)"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dd2+datetime.timedelta( 1)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "7adf4afd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"0\n",
"0\n",
"1\n",
"1\n",
"1\n",
"0\n",
"0\n",
"0\n"
]
}
],
"source": [
"import numpy as np\n",
"for i in range (10):\n",
" print(np.random.randint(0,2))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85f60b71",
"metadata": {},
"outputs": [],
"source": []
}
],
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment