1
0
mirror of https://github.com/EliasKotlyar/Xiaomi-Dafang-Hacks.git synced 2025-09-09 07:30:57 +02:00

Motion tracking (#216)

* Debug information on OSD
Add some debug info on OSD:
 * Hostname
 * IP
 * Transmitted bandwith
 * Rec bandwith
 * CPU usage (based on top command)
 * Freemem (based on free command)
 * Used memory (based on top command)
 * Free memory (based on top command)
This is controlable from scripts/debugInOsd.sh

* Motion tracking !
- Add motion tracking (basic)
- Need to add configuration
- Did small memory optimisation (no allocate memory for OSD if not needed)
This commit is contained in:
nik0
2018-04-11 20:35:27 +02:00
committed by Elias Kotlyar
parent d2b84a9fdd
commit 47073bfed5
9 changed files with 315 additions and 28 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -4,9 +4,11 @@
# Configure motion core parameters
region_of_interest=0,0,1280,720
region_of_interest=0,0,0,0
motion_sensitivity=4
motion_indicator_color=2
motion_timeout=-1
motion_tracking=off
# Configure how to deal with motion events

View File

@@ -8,4 +8,5 @@ if [ -f /system/sdcard/config/motion.conf ] ; then
/system/sdcard/bin/setconf -k r -v ${region_of_interest} 2>/dev/null
/system/sdcard/bin/setconf -k m -v ${motion_sensitivity} 2>/dev/null
/system/sdcard/bin/setconf -k z -v ${motion_indicator_color} 2>/dev/null
/system/sdcard/bin/setconf -k t -v ${motion_tracking} 2>/dev/null
fi;

View File

@@ -0,0 +1,198 @@
#!/bin/sh
# Script to calculate the camera movement
# The screen is split as below (0,1,2,3 are the parameters of the script)
# +--------------------------------------+
# | | |
# | 0 | 1 |
# | | |
# +--------------------------------------+
# | | |
# | 2 | 3 |
# | | |
# +--------------------------------------+
STEPS=100
FILECAMERAPOS=/tmp/cameraposition
motorLeft(){
/system/sdcard/bin/motor -d l -s ${1}
}
motorRight(){
/system/sdcard/bin/motor -d r -s ${1}
}
motorUp() {
/system/sdcard/bin/motor -d u -s ${1}
}
motorDown() {
/system/sdcard/bin/motor -d d -s ${1}
}
backtoOrigin() {
# return to origin for both axis
# Get values in saved config file
if [ -f ${FILECAMERAPOS} ]; then
origin_x_axis=`grep "x_steps:" ${FILECAMERAPOS} | sed "s/x_steps: //"`
origin_y_axis=`grep "y_steps:" ${FILECAMERAPOS} | sed "s/y_steps: //"`
else
origin_x_axis=0
origin_y_axis=0
fi
# Get the current position
x_axis=`/system/sdcard/bin/motor -d s | grep "x_steps:" | sed "s/x_steps: //"`
y_axis=`/system/sdcard/bin/motor -d s | grep "y_steps:" | sed "s/y_steps: //"`
#--------------------------- X Position -------------------------------------------
#Calculate the difference between the origin and the position now
if [ ${origin_x_axis} -lt ${x_axis} ]
then
diff=$((${origin_x_axis} - ${x_axis}))
#This is a substitution trick to take the abs number
diff=${diff#-}
motorLeft "${diff}"
else
diff=$((${x_axis} - ${origin_x_axis}))
#This is a substitution trick to take the abs number
diff=${diff#-}
motorRight "${diff}"
fi
# Let some time for the motor to turn
sleep 1
#--------------------------- Y Position -------------------------------------------
#Calculate the difference between the origin and the position now
if [ ${origin_y_axis} -lt ${y_axis} ]
then
diff=$((${origin_y_axis} - ${y_axis}))
#This is a substitution trick to take the abs number
diff=${diff#-}
motorDown "${diff}"
else
diff=$((${y_axis} - ${origin_y_axis}))
#This is a substitution trick to take the abs number
diff=${diff#-}
motorUp "${diff}"
fi
# Let some time for the motor to turn
sleep 1
}
#################### Start ###
# If no argument that's mean the camera need to return to its original position
# the 5th arguments is '&'
if [ $# -ne 5 ]
then
backtoOrigin
return 0;
fi
# Now save the "origin" values of the camera
# Save it to tmp as when the camera reboots the camera are set to 0
if [ -f ${FILECAMERAPOS} ]; then
/system/sdcard/bin/motor -d s > ${FILECAMERAPOS}
fi
UP=0
DOWN=0
LEFT=0
RIGHT=0
# Display the areas ...
echo $1 $2
echo $3 $4
# Sum all the parameters, that gives the number of region detected
# Only 2 are supported
if [ $((${1} + ${2} + ${3} +${4})) -gt 2 ]
then
echo "No move if more than 3 detected regions"
return 0
fi
# Basic algorithm to calculate the movement
# Not optimized, if you have ideas to simplify it ...
if [ "${1}" == "1" ] && [ "${2}" == "1" ]
then
UP=1
elif [ "${1}" == "1" ] && [ "${3}" == "1" ]
then
LEFT=1
elif [ "${2}" == "1" ] && [ "${4}" == "1" ]
then
RIGHT=1
elif [ "${3}" == "1" ] && [ "${4}" == "1" ]
then
DOWN=1
elif [ "${1}" == "1" ]
then
UP=1
LEFT=1
elif [ "${2}" == "1" ]
then
UP=1
RIGHT=1
elif [ "${3}" == "1" ]
then
LEFT=1
DOWN=1
elif [ "${4}" == "1" ]
then
RIGHT=1
DOWN=1
fi
# Some sanity checks
if [ "${UP}" != 0 ] && [ "${DOWN}" != 0 ]
then
echo "no move: up and down at the same time"
return 0
fi
if [ "${RIGHT}" != 0 ] && [ "${LEFT}" != 0 ]
then
echo "no move: right and left at the same time"
return 0
fi
if [ ${RIGHT} != 0 ]
then
echo "Right move"
motorRight ${STEPS}
fi
if [ ${LEFT} != 0 ]
then
echo "Left move"
motorLeft ${STEPS}
fi
if [ ${UP} != 0 ]
then
echo "Up move"
motorUp ${STEPS}
fi
if [ ${DOWN} != 0 ]
then
echo "Down move"
motorDown ${STEPS}
fi

View File

@@ -248,6 +248,15 @@ if [ -n "$F_cmd" ]; then
rewrite_config /system/sdcard/config/motion.conf region_of_interest "${F_x0},${F_y0},${F_x1},${F_y1}"
rewrite_config /system/sdcard/config/motion.conf motion_sensitivity "${F_motion_sensitivity}"
rewrite_config /system/sdcard/config/motion.conf motion_indicator_color "${F_motion_indicator_color}"
rewrite_config /system/sdcard/config/motion.conf motion_timeout "${F_motion_timeout}"
if [ "${F_motion_tracking}X" == "X" ]
then
rewrite_config /system/sdcard/config/motion.conf motion_tracking off
/system/sdcard/bin/setconf -k t -v off
else
rewrite_config /system/sdcard/config/motion.conf motion_tracking on
/system/sdcard/bin/setconf -k t -v on
fi
# echo "region_of_interest=${F_x0},${F_y0},${F_x1},${F_y1}" > /system/sdcard/config/motion.conf
# echo "motion_sensitivity=${F_motion_sensitivity}" >> /system/sdcard/config/motion.conf
@@ -256,10 +265,12 @@ if [ -n "$F_cmd" ]; then
/system/sdcard/bin/setconf -k r -v ${F_x0},${F_y0},${F_x1},${F_y1}
/system/sdcard/bin/setconf -k m -v ${F_motion_sensitivity}
/system/sdcard/bin/setconf -k z -v ${F_motion_indicator_color}
/system/sdcard/bin/setconf -k u -v ${F_motion_timeout}
# Changed the detection region, need to restart the server
if [ ${F_restart_server} == "1" ]
then
processName="v4l2rtspserver-master"
#get the process pid
processId=`ps | grep ${processName} | grep -v grep | awk '{ printf $1 }'`
@@ -268,14 +279,18 @@ if [ -n "$F_cmd" ]; then
#found the process, now get the full path and the parameters in order to restart it
executable=`ls -l /proc/${processId}/exe | awk '{print $NF}'`
cmdLine=`tr '\0' ' ' < /proc/${processId}/cmdline | awk '{$1=""}1'`
kill ${processId} 2>/dev/null
# Set the socket option in order to restart easily the server (socket in use)
echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
sleep 2
cmdLine="/system/sdcard/bin/busybox nohup "${executable}${cmdLine} 2>/dev/null
${cmdLine} &>/dev/null
${cmdLine} 2>/dev/null >/dev/null &
else
echo "process v4l2rtspserver-master was not found"
echo "<BR>"
fi
fi

View File

@@ -23,6 +23,20 @@ then
fi
echo "region_of_interest=[${region_of_interest}];"
motion_tracking=`/system/sdcard/bin/setconf -g t 2>/dev/null`
if [ "${motion_tracking}X" == "X" ]
then
motion_tracking=false
fi
echo "motion_tracking=${motion_tracking};"
motion_timeout=`/system/sdcard/bin/setconf -g u 2>/dev/null`
if [ "${motion_timeout}X" == "X" ]
then
motion_tracking=60
fi
echo "motion_timeout=${motion_timeout};"

View File

@@ -52,6 +52,16 @@ http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
overflow: hidden;
word-break: normal;
}
.img h2 {
position: absolute;
top: 20%;
left: 20%;
font-size: 28pt;
text-align: center;
background-color: #B0E0E6
}
</style>
<script type="text/javascript">
@@ -69,6 +79,37 @@ http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
$('#restart_server').val(1);
}
function setTracking(obj,init) {
var ias = $('#picture').imgAreaSelect({
instance: true
});
if($(obj).is(":checked")){
ias.setOptions({
hide: true
});
$('#picture').css('opacity',0.5);
$("#RegionDisabled").show();
document.getElementById('motion_timeout').readOnly = false;
document.getElementById('motion_timeout').style.backgroundColor = "#FFFFFF";
}else{
ias.setOptions({
show: true
});
$('#picture').css('opacity',1);
$("#RegionDisabled").hide();
document.getElementById('motion_timeout').readOnly = true;
document.getElementById('motion_timeout').style.backgroundColor = "#B0E0E6";
}
ias.update();
if (init == false) {
$('#restart_server').val(1);
}
}
$(function() {
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET', 'cgi-bin/getMotionInfo.cgi', false);
@@ -84,15 +125,16 @@ http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
var ias = $('#picture').imgAreaSelect({
instance: true
});
ias.setOptions({
show: true
});
ias.update();
if (region_of_interest[0] == 0 && region_of_interest[1] == 0 && region_of_interest[2] == 0 && region_of_interest[3] == 0)
ias.setSelection(0, 0, width, height, true);
if ((region_of_interest[0] == 0 && region_of_interest[1] == 0 && region_of_interest[2] == 0 && region_of_interest[3] == 0) ||
(region_of_interest[0] > width || region_of_interest[1] > height || region_of_interest[2] > width || region_of_interest[3] > height))
{
ias.setSelection(0, 0, width, height, true);
}
else
{
ias.setSelection(region_of_interest[0], region_of_interest[1], region_of_interest[2], region_of_interest[3], false);
}
$('#x0').val(region_of_interest[0]);
$('#y0').val(region_of_interest[1]);
@@ -101,7 +143,15 @@ http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
$('#restart_server').val(0);
$('#motion_indicator_color').val(motion_indicator_color).change();
$('#motion_sensitivity').val(motion_sensitivity).change();
console.log(motion_tracking);
$('#motion_tracking').prop('checked',motion_tracking);
$('#motion_timeout').val(motion_timeout).change();
setTracking( $('#motion_tracking').get(0), true);
});
</script>
@@ -114,8 +164,11 @@ http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Select motion detection region (changing region will restart the rtsp server)
</p>
<div class="frame" style="margin: 0 0.3em;">
<div class="img" style="margin: 0 0.3em;">
<img id="picture" src="/cgi-bin/currentpic.cgi" />
<span id="RegionDisabled">
<h2> Region disabled</h2>
</span>
</div>
<form style="margin: 0px" action="/cgi-bin/action.cgi?cmd=set_region_of_interest" method="post">
<input id="x0" name="x0" type="hidden" />
@@ -124,27 +177,31 @@ http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<input id="y1" name="y1" type="hidden" />
<input id="restart_server" name="restart_server" type="hidden" /> Set sensitivity or deactivate
<select name="motion_sensitivity" id="motion_sensitivity">
<option value="-1">Motion deactivated</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<option value="-1">Motion deactivated</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<br>Display motion indicator if motion is detected
<select name="motion_indicator_color" id="motion_indicator_color">
<option value="-1">Deactivated</option>
<option value="0">White</option>
<option value="1">Black</option>
<option value="2">Red</option>
<option value="3">Green</option>
<option value="4">Blue</option>
<option value="5">Cyan</option>
<option value="6">Yellow</option>
<option value="7">Purple</option>
</select>
<option value="-1">Deactivated</option>
<option value="0">White</option>
<option value="1">Black</option>
<option value="2">Red</option>
<option value="3">Green</option>
<option value="4">Blue</option>
<option value="5">Cyan</option>
<option value="6">Yellow</option>
<option value="7">Purple</option>
</select>
<br>
Enable motion tracking (when enabled no region of interest is no more used)
<input type="checkbox" name="motion_tracking" id="motion_tracking" onchange="setTracking(this, false)">
<br> Motion Timeout (restore camera position after x seconds) -1 to deactivate
<input id="motion_timeout" name="motion_timeout" type="number" size="6">
<br>
<input type="submit" value="Configure motion" />