Having problem in video stegano, the hiden message always lost - python

friend, i am currently looking for a way to video stegano. I successfully in splitting frames from video file and hide messages inside them. But when i combine these frames into video and trying to extract info from the hiden video, i always failed. I guess here is problem with video compression.
Here is my code.
from stegano import lsb
from os.path import isfile, join
import time # install time ,opencv,numpy modules
import cv2
import numpy as np
import math
import os
import shutil
from moviepy.editor import *
from subprocess import call, STDOUT
def split_string(s_str, count=10):
per_c = math.ceil(len(s_str)/count)
c_cout = 0
out_str = ''
split_list = []
for s in s_str:
out_str += s
c_cout += 1
if c_cout == per_c:
split_list.append(out_str)
out_str = ''
c_cout = 0
if c_cout != 0:
split_list.append(out_str)
return split_list
def frame_extraction(video):
if not os.path.exists("./tmp"):
os.makedirs("tmp")
temp_folder = "./tmp"
print("[INFO] tmp directory is created")
vidcap = cv2.VideoCapture(video)
count = 0
while True:
success, image = vidcap.read()
if not success:
break
cv2.imwrite(os.path.join(temp_folder, "{:d}.png".format(count)), image)
count += 1
print("[INFO] frame {} is extracted".format(count))
def encode_string(input_string, root="./tmp/"):
split_string_list = split_string(input_string)
for i in range(0, len(split_string_list)):
f_name = "{}{}.png".format(root, i)
secret_enc = lsb.hide(f_name, split_string_list[i])
secret_enc.save(f_name)
print("[INFO] frame {} holds {}".format(f_name, lsb.reveal(f_name)))
def decode_string(video):
frame_extraction(video)
secret = []
root = "./tmp/"
for i in range(len(os.listdir(root))):
f_name = "{}{}.png".format(root, i)
print("[INFO] frame {} is decoding".format(f_name))
secret_dec = lsb.reveal(f_name)
if secret_dec == None:
break
secret.append(secret_dec)
print("[INFO] secret is {}".format("".join(secret)))
print(''.join([i for i in secret]))
# clean_tmp()
def clean_tmp(path="./tmp"):
if os.path.exists(path):
shutil.rmtree(path)
print("[INFO] tmp files are cleaned up")
def main():
input_string = input("Enter the input string: ")
f_name = input("enter the name of video: ")
# 从源文件分离出帧
frame_extraction(f_name)
# 分离文件路径和扩展名
file_path, file_extraction = os.path.splitext(f_name)
# 创建输出音频文件
audio_path = file_path + "_temp.mp3"
video = VideoFileClip(f_name)
video.audio.write_audiofile(audio_path)
# 加密字符
encode_string(input_string)
# 从tmp文件夹的图片创建没有声音的视频
fps=30
img_root = r"./tmp/"
# fourcc = cv2.VideoWriter_fourcc(*'mp4v')
fourcc = cv2.VideoWriter_fourcc(*'XVID')
video_file_path = file_path + "_temp.avi"
# 获取tmp文件夹第一张视频的尺寸
img = cv2.imread(img_root + "0.png")
height, width, layers = img.shape
size=(width,height)
videoWriter = cv2.VideoWriter(video_file_path,fourcc=fourcc,fps=fps,frameSize=size)
for i in range(len(os.listdir(img_root))):
frame = cv2.imread(img_root+str(i)+'.png')
videoWriter.write(frame)
videoWriter.release()
# 合并视频和音频 audio_path video_file_path
video = VideoFileClip(video_file_path)
audio_clip = AudioFileClip(audio_path)
video = video.set_audio(audio_clip)
video.write_videofile(file_path + "_hide.avi")
clean_tmp()
if __name__ == "__main__":
while True:
print("1.Hide a message in video 2.Reveal the secret from video")
print("any other value to exit")
choice = input()
if choice == '1':
main()
elif choice == '2':
decode_string(input("enter the name of video with extension: "))
else:
break
I have tried mp4, avi, wov format. But none of them worked.
IF YOU HAVE ANY IDEA OR SUGGESTION GIVEN TO ME, I WOULD BE VERY GRATEFUL

Related

How to Deploy Frame differencing python file , to get an endpoint?

I want to exact frames from a screen recorded video file, and those frames are used to object detection process later. I already have a python code which can simply exact frames and save them inside my local storage.
import os
import numpy as np
import cv2
from glob import glob
def create_dir(path):
try:
if not os.path.exists(path):
os.makedirs(path)
except OSError:
print(f"ERROR: creating directory with name {path}")
def save_frame(video_path, save_dir, gap=10):
name = video_path.split("/")[-1].split(".")[0]
save_path = os.path.join(save_dir, name)
create_dir(save_path)
cap = cv2.VideoCapture(video_path)
idx = 0
while True:
ret, frame = cap.read()
if ret == False:
cap.release()
break
if idx == 0:
cv2.imwrite(f"{save_path}/{idx}.png", frame)
else:
if idx % gap == 0:
cv2.imwrite(f"{save_path}/{idx}.png", frame)
idx += 1
if __name__ == "__main__":
video_paths = glob("C:/Users/Asus/Documents/4th year/Research/Frame Differencing/video to frame/capture1.mp4")
save_dir = "save"
for path in video_paths:
save_frame(path, save_dir, gap=50)
I want to deploy this to get endpoint (HTTP request). The Request input should be a video file and response should be frames of it. Please help me to deploy this

How to integrate OpenCV webcam with Django and React?

I have a barcode reader which I implemented by using opensource Dynamsoft API and OpenCV.
Now I need to integrate it with Django and display on my website in React.
I have no idea how to do that, I tried passing the code to my views.py but don't know what I should do next.
Here is my code for barcode reading:
import cv2
from dbr import *
import time
reader = BarcodeReader()
def text_results_callback_func(frame_id, t_results, user_data):
print(frame_id)
for result in t_results:
text_result = TextResult(result)
print("Barcode Format : ")
print(text_result.barcode_format_string)
print("Barcode Text : ")
print(text_result.barcode_text)
print("Exception : ")
print(text_result.exception)
print("-------------")
def get_time():
localtime = time.localtime()
capturetime = time.strftime("%Y%m%d%H%M%S", localtime)
return capturetime
def read_barcode():
video_width = 0
video_height = 0
vc = cv2.VideoCapture(0)
video_width = vc.get(cv2.CAP_PROP_FRAME_WIDTH)
video_height = vc.get(cv2.CAP_PROP_FRAME_HEIGHT)
vc.set(3, video_width)
vc.set(4, video_height)
stride = 0
if vc.isOpened():
rval, frame = vc.read()
stride = frame.strides[0]
else:
return
windowName = "Barcode Reader"
parameters = reader.init_frame_decoding_parameters()
parameters.max_queue_length = 30
parameters.max_result_queue_length = 30
parameters.width = video_width
parameters.height = video_height
parameters.stride = stride
parameters.image_pixel_format = EnumImagePixelFormat.IPF_RGB_888
parameters.region_top = 0
parameters.region_bottom = 100
parameters.region_left = 0
parameters.region_right = 100
parameters.region_measured_by_percentage = 1
parameters.threshold = 0.01
parameters.fps = 0
parameters.auto_filter = 1
reader.start_video_mode(parameters, text_results_callback_func)
while True:
cv2.imshow(windowName, frame)
rval, frame = vc.read()
if rval == False:
break
try:
ret = reader.append_video_frame(frame)
except:
pass
key = cv2.waitKey(1)
if key == ord('q'):
break
reader.stop_video_mode()
cv2.destroyWindow(windowName)
print("-------------------start------------------------")
reader.init_license("***************************")
read_barcode()
I think you need to use the JS version of Dynamsoft Barcode Reader to scan barcodes using cameras in a webpage: https://www.dynamsoft.com/barcode-reader/sdk-javascript/

How to capture video by video from one rtsp Url using Opencv?

The server is sending video by video using the same RTSP URL(rtsp://192.168.0.2:8554/)
I can capture and display video using opencv.
import numpy as np
import cv2 as cv
os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"] = "rtsp_transport;udp"
cap = cv.VideoCapture('rtsp://192.168.0.2:8554/')
while cap.isOpened():
ret, frame = cap.read()
# if frame is read correctly ret is True
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
cv.imshow('frame', frame)
if cv.waitKey(1) == ord('q'):
break
cap.release()
cv.destroyAllWindows()
This program returns error when going on to the next video.
I tried this, but this didn't work.
import cv2 as cv
import os
import time
os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"] = "rtsp_transport;udp"
cap = cv.VideoCapture('rtsp://192.168.0.26:8554/')
if not cap.isOpened():
print("Cannot open camera")
exit()
while True:
try:
time.sleep(2)
# Capture frame-by-frame
ret, frame = cap.read()
# if frame is read correctly ret is True
# Our operations on the frame come here
# Display the resulting frame
cv.imshow('frame',frame)
if cv.waitKey(1) == ord('q'):
break
except:
print("Exception!!")
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()
Can I get some help?
Thanks in advance!
I solved this by using multi-threaded program.
Main file
from datasets import LoadStreams
import threading
import os
import logging
import cv2
import torch
import time
logger = logging.getLogger(__name__)
def select_device(device='', batch_size=None):
# device = 'cpu' or '0' or '0,1,2,3'
cpu_request = device.lower() == 'cpu'
if device and not cpu_request: # if device requested other than 'cpu'
os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable
assert torch.cuda.is_available(), f'CUDA unavailable, invalid device {device} requested' # check availablity
cuda = False if cpu_request else torch.cuda.is_available()
if cuda:
c = 1024 ** 2 # bytes to MB
ng = torch.cuda.device_count()
if ng > 1 and batch_size: # check that batch_size is compatible with device_count
assert batch_size % ng == 0, f'batch-size {batch_size} not multiple of GPU count {ng}'
x = [torch.cuda.get_device_properties(i) for i in range(ng)]
s = f'Using torch {torch.__version__} '
for i, d in enumerate((device or '0').split(',')):
if i == 1:
s = ' ' * len(s)
logger.info(f"{s}CUDA:{d} ({x[i].name}, {x[i].total_memory / c}MB)")
else:
logger.info(f'Using torch {torch.__version__} CPU')
logger.info('') # skip a line
return torch.device('cuda:0' if cuda else 'cpu')
def detect(rtsp_url):
dataset = LoadStreams(rtsp_url)
device = select_device('')
count = 0
view_img = True
# img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img
try:
for frame_idx, (path, img, im0s, vid_cap) in enumerate(dataset): # for every frame
count += 1
im0 = im0s[0].copy()
if view_img:
cv2.imshow(str(path), im0)
# if cv2.waitKey(1) == ord('q'): # q to quit
# raise StopIteration
except:
print("finish execption")
dataset.stop()
return "good"
if __name__ == '__main__':
rtsp_url = "rtsp://192.168.0.26:8554/"
while True:
for thread in threading.enumerate():
print(thread.name)
print(detect(rtsp_url))
dataset class file
import glob
import logging
import math
import os
import random
import shutil
import time
import re
from itertools import repeat
from multiprocessing.pool import ThreadPool
from pathlib import Path
from threading import Thread
import cv2
import numpy as np
import torch
class LoadStreams: # multiple IP or RTSP cameras
def __init__(self, sources='streams.txt', img_size=640):
self.mode = 'stream'
self.img_size = img_size
self.capture = None
self.my_thread = None
self.stopFlag = False
if os.path.isfile(sources):
with open(sources, 'r') as f:
sources = [x.strip() for x in f.read().strip().splitlines() if len(x.strip())]
else:
sources = [sources]
n = len(sources)
self.imgs = [None] * n
self.sources = [clean_str(x) for x in sources] # clean source names for later
s = sources[0]
# for i, s in enumerate(sources):
# Start the thread to read frames from the video stream
# print('%g/%g: %s... ' % (i + 1, n, s), end='')
cap = cv2.VideoCapture(eval(s) if s.isnumeric() else s)
assert cap.isOpened(), 'Failed to open %s' % s
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS) % 100
self.ret, self.imgs[0] = cap.read() # guarantee first frame
thread = Thread(target=self.update, args=([0, cap]), daemon=True)
print(' success (%gx%g at %.2f FPS).' % (w, h, fps))
thread.start()
self.capture = cap
self.my_thread = thread
print('') # newline
# check for common shapes
s = np.stack([letterbox(x, new_shape=self.img_size)[0].shape for x in self.imgs], 0) # inference shapes
self.rect = np.unique(s, axis=0).shape[0] == 1 # rect inference if all shapes equal
if not self.rect:
print('WARNING: Different stream shapes detected. For optimal performance supply similarly-shaped streams.')
def update(self, index, cap):
# Read next stream frame in a daemon thread
n = 0
while cap.isOpened() and not self.stopFlag:
n += 1
# _, self.imgs[index] = cap.read()
cap.grab()
if n == 4: # read every 4th frame
_, self.imgs[index] = cap.retrieve()
n = 0
time.sleep(0.01) # wait time
def stop(self):
self.stopFlag = True
try:
# self.capture.release()
# self.my_thrsead.join()
print("stop thread!!")
except:
print("ERROR stopping thread!!")
def __iter__(self):
self.count = -1
return self
def __next__(self):
self.count += 1
img0 = self.imgs.copy()
if cv2.waitKey(1) == ord('q'): # q to quit
cv2.destroyAllWindows()
raise StopIteration
if not self.ret:
print("error!!!")
self.stop()
# Letterbox
img = [letterbox(x, new_shape=self.img_size, auto=self.rect)[0] for x in img0]
# Stack
img = np.stack(img, 0)
# Convert
img = img[:, :, :, ::-1].transpose(0, 3, 1, 2) # BGR to RGB, to bsx3x416x416
img = np.ascontiguousarray(img)
return self.sources, img, img0, None
def __len__(self):
return 0 # 1E12 frames = 32 streams at 30 FPS for 30 years
# def stop(self):
def clean_str(s):
# Cleans a string by replacing special characters with underscore _
return re.sub(pattern="[|##!¡·$€%&()=?¿^*;:,¨´><+]", repl="_", string=s)
def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True):
# Resize image to a 32-pixel-multiple rectangle https://github.com/ultralytics/yolov3/issues/232
shape = img.shape[:2] # current shape [height, width]
if isinstance(new_shape, int):
new_shape = (new_shape, new_shape)
# Scale ratio (new / old)
r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
if not scaleup: # only scale down, do not scale up (for better test mAP)
r = min(r, 1.0)
# Compute padding
ratio = r, r # width, height ratios
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding
if auto: # minimum rectangle
dw, dh = np.mod(dw, 32), np.mod(dh, 32) # wh padding
elif scaleFill: # stretch
dw, dh = 0.0, 0.0
new_unpad = (new_shape[1], new_shape[0])
ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios
dw /= 2 # divide padding into 2 sides
dh /= 2
if shape[::-1] != new_unpad: # resize
img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR)
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border
return img, ratio, (dw, dh)
while cap.isOpened() and not self.stopFlag:
this line is especially important because
without this line the threads will be stacked and will have memory error
as the stack stacks up.

Audio out of synch when combining video and audio with moviepy

I wrote myself a little youtube downloader for practice, that can download video and audio. So the problem is, that the formats Full HD <= mostly don't contain any audio. So my solution for that is to just download the audio seperatly and combine it together afterwards with moviepy. My problem is that when I watch the final output the audio is about .5 seconds behind the video, which looks really weird. The code (its bad, but its just an exercise):
from pytube import YouTube
from moviepy.editor import VideoFileClip
from time import sleep
import os
def convert_to_mp3(filename):#method to convert to mp3 since i havent found a way to download mp3
clip = VideoFileClip(filename)
clip.audio.write_audiofile(filename[:-4] + ".mp3")
clip.close()
def input_link():#get the yt link
print("Videolink:")
link = input()
return link
while True:#check the link until its correct
try:
link = input_link()
video = YouTube(link)
break
except Exception:
print("Fehler im Link!\n")
print("\n1) Video\n2) Audio")
def download_video(itag, name=video.title):#download the video and return the type
# print(video.streams.filter(file_extension="mp4").all)
name = video.streams.get_by_itag(itag).download(filename=name)
if name.__contains__(".webm"):
return "webm"
elif name.__contains__(".mp4"):
return "mp4"
else:
print("Unknown format... Name of the file: " + name)
return "unknown"
def combine_audio(vidname, audname, outname, fps=25):#combine the audio and the video with moviepy
import moviepy.editor as mpe
my_clip = mpe.VideoFileClip(vidname)
audio_background = mpe.AudioFileClip(audname)
final_clip = my_clip.set_audio(audio_background)
final_clip.write_videofile(outname)
print("removing " + vidname)
os.remove(vidname)
print("removing " + audname)
os.remove(audname)
video_audio = input()
def download_video_only(download=True):#function to download the video
global fps
formats = str(video.streams.filter().all)
formatsFormattet = formats[formats.index("[") + 1:formats.index("]")]
formatsList = []
while formatsFormattet != "":#prepares the list of the available formats, ill make a method to sort out doubles later
try:
formatsList.append(formatsFormattet[formatsFormattet.index(":") + 1:formatsFormattet.index(",") + 1])
except ValueError:
formatsList.append(formatsFormattet[formatsFormattet.index(":") + 1:len(formatsFormattet)])
try:
formatsFormattet = formatsFormattet[formatsFormattet.index(",") + 1: len(formatsFormattet)]
except ValueError:
formatsFormattet = ""
counter = 1
itagList = []
for element in formatsList:
try:
element = element[element.index("itag=") + 6:len(element)]
if element[2] == '"':
itagList.append(element[0:2])
else:
itagList.append(element[0:3])
# print("\nItag: " + element[0:3])
element = element[element.index("res=") + 5:len(element)]
print("\n" + str(counter) + ")\nAuflösung: " + element[0:element.index("\"")])
element = element[element.index("fps=") + 5:len(element)]
fps = element[0:element.index("\"")]
print("Fps: " + element[0:element.index("\"")])
counter += 1
except ValueError:
break
counter -= 1
while True:
print("\nWelches Format hätten sie gerne(1-" + str(counter)+ ")")#print all the formats for the user to select one
selection = input()
if int(selection) - 1 < len(formatsList):
# print(formatsList[int(selection) - 1])
if download:
if video.streams.get_by_itag(itagList[int(selection) - 1]).is_progressive:#if the video has audio, just download it
download_video(itagList[int(selection) - 1])
else:#if the video has no audio, download audio and video and combine it after finding out the format
download_audio_only(name="audio")
type = download_video(itagList[int(selection) - 1], name="video")
if(type == "webm"):
combine_audio("video.webm", "audio.mp3", video.title + ".mp4", fps)
elif type == "mp4":
combine_audio("video.mp4", "audio.mp3", video.title + ".mp4", fps)
elif type == "unknown":
return
else:
return itagList[int(selection) - 1]
break
print("\nUngültige Eingabe, bitte richtige Eingabe tätigen")
def download_audio_only(name=video.title):#download the audio and make it to mp3
file = video.streams.filter(only_audio=True).first().download(filename=name)
sleep(1)
print(file)
os.rename("audio.mp4", "audio.mp3")
if int(video_audio) == 1:#get selection for video or audio
download_video_only()
else:
download_audio_only()
Any ideas on how to fix that?
Thanks for any help

Python FTP Upload - Errorno -2, Name or service not known

i am implementing a FTP upload with my python script. Everything works except for my ftp upload. I have used chmod to change the permissions of the folder that i have got my images and script in to chmod -R 777. And the permissions on the FTP server are all set at 777. Please note that ftpDir is intentionally left blank as this is the root folder for the account on the ftp server. I do not know what else i can do to fix this issue. 3
This is my error code that i get:
placeFile - Start FTP to ftp.brisbaneskycams.com
placeFile - ERROR FTP transfer Failed ...
Filename : /home/pi/pimotion/images/capture-20160503-044806.jpg
Error Msg: ['[Errno', '-2] Name or service not known']
Please Investigate Problem ...
This is the script:
#!/usr/bin/python
#
# Lightweight Motion Detection using python picamera libraries
# based on code from raspberry pi forum by user utpalc
# modified by Claude Pageau for this working example
# ------------------------------------------------------------
# original code on github https://github.com/pageauc/picamera-motion
# This is sample code that can be used for further development
verbose = True
if verbose:
print "Loading python libraries ....."
else:
print "verbose output has been disabled verbose=False"
import picamera
import picamera.array
import datetime
import time
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
from fractions import Fraction
import ftplib
from ftplib import FTP
import os
#Constants
SECONDS2MICRO = 1000000 # Constant for converting Shutter Speed in Seconds to Microseconds
# User Customizable Settings
imageDir = "images"
imagePath = "/home/pi/pimotion/" + imageDir
imageNamePrefix = 'capture-' # Prefix for all image file names. Eg front-
imageWidth = 1980
imageHeight = 1080
imageVFlip = False # Flip image Vertically
imageHFlip = False # Flip image Horizontally
imagePreview = False
# FTP Server location variables
ftpOn = True
ftpServer = "ftphostname"
ftpDir = ""
ftpUser = "ftpusername"
ftpPass = "XXXXXXXXXX"
numberSequence = False
threshold = 10 # How Much pixel changes
sensitivity = 100 # How many pixels change
nightISO = 800
nightShutSpeed = 6 * SECONDS2MICRO # seconds times conversion to microseconds constant
# Advanced Settings not normally changed
testWidth = 100
testHeight = 75
def checkImagePath(imagedir):
# Find the path of this python script and set some global variables
mypath=os.path.abspath(__file__)
baseDir=mypath[0:mypath.rfind("/")+1]
baseFileName=mypath[mypath.rfind("/")+1:mypath.rfind(".")]
# Setup imagePath and create folder if it Does Not Exist.
imagePath = baseDir + imagedir # Where to save the images
# if imagePath does not exist create the folder
if not os.path.isdir(imagePath):
if verbose:
print "%s - Image Storage folder not found." % (progName)
print "%s - Creating image storage folder %s " % (progName, imagePath)
os.makedirs(imagePath)
return imagePath
def takeDayImage(imageWidth, imageHeight, filename):
if verbose:
print "takeDayImage - Working ....."
with picamera.PiCamera() as camera:
camera.resolution = (imageWidth, imageHeight)
# camera.rotation = cameraRotate #Note use imageVFlip and imageHFlip variables
if imagePreview:
camera.start_preview()
camera.vflip = imageVFlip
camera.hflip = imageHFlip
# Day Automatic Mode
camera.exposure_mode = 'auto'
camera.awb_mode = 'auto'
camera.capture(filename)
def placeFile(filepath):
path, filename = os.path.split(filepath)
os.chdir(path)
print filename
print path
if verbose:
print("placeFile - Start FTP to %s" % ftpServer )
try:
ftp = FTP(ftpServer)
ftp.login(user=ftpUser, passwd = ftpPass)
ftp.cwd(ftpDir)
ftp.storbinary('STOR ' + filename, open(filename, 'rb'))
ftp.quit()
if verbose:
print("placeFile - SUCCESSFUL FTP Transfer")
print(" Filename : %s " % (filepath))
except ftplib.all_errors as e:
errorcode_string = str(e).split(None, 1)
if verbose:
print("placeFile - ERROR FTP transfer Failed ...")
print(" Filename : %s " % (filepath))
print(" Error Msg: %s" % ( errorcode_string ))
print(" Please Investigate Problem ...")
def takeNightImage(imageWidth, imageHeight, filename):
if verbose:
print "takeNightImage - Working ....."
with picamera.PiCamera() as camera:
camera.resolution = (imageWidth, imageHeight)
if imagePreview:
camera.start_preview()
camera.vflip = imageVFlip
camera.hflip = imageHFlip
# Night time low light settings have long exposure times
# Settings for Low Light Conditions
# Set a frame rate of 1/6 fps, then set shutter
# speed to 6s and ISO to approx 800 per nightISO variable
camera.framerate = Fraction(1, 6)
camera.shutter_speed = nightShutSpeed
camera.exposure_mode = 'off'
camera.iso = nightISO
# Give the camera a good long time to measure AWB
# (you may wish to use fixed AWB instead)
time.sleep(10)
camera.capture(filename)
if verbose:
print "checkNightMode - Captured %s" % (filename)
return filename
def takeMotionImage(width, height, daymode):
with picamera.PiCamera() as camera:
time.sleep(1)
camera.resolution = (width, height)
with picamera.array.PiRGBArray(camera) as stream:
if daymode:
camera.exposure_mode = 'auto'
camera.awb_mode = 'auto'
else:
# Take Low Light image
# Set a framerate of 1/6 fps, then set shutter
# speed to 6s and ISO to 800
camera.framerate = Fraction(1, 6)
camera.shutter_speed = nightShutSpeed
camera.exposure_mode = 'off'
camera.iso = nightISO
# Give the camera a good long time to measure AWB
# (you may wish to use fixed AWB instead)
time.sleep( 10 )
camera.capture(stream, format='rgb')
return stream.array
def scanIfDay(width, height, daymode):
data1 = takeMotionImage(width, height, daymode)
while not motionFound:
data2 = takeMotionImage(width, height, daymode)
pCnt = 0L;
diffCount = 0L;
for w in range(0, width):
for h in range(0, height):
# get the diff of the pixel. Conversion to int
# is required to avoid unsigned short overflow.
diff = abs(int(data1[h][w][1]) - int(data2[h][w][1]))
if diff > threshold:
diffCount += 1
if diffCount > sensitivity:
break; #break outer loop.
if diffCount > sensitivity:
motionFound = True
else:
# print "Sum of all pixels=", pxCnt
data2 = data1
return motionFound
def scanMotion(width, height, daymode):
motionFound = False
data1 = takeMotionImage(width, height, daymode)
while not motionFound:
data2 = takeMotionImage(width, height, daymode)
diffCount = 0L;
for w in range(0, width):
for h in range(0, height):
# get the diff of the pixel. Conversion to int
# is required to avoid unsigned short overflow.
diff = abs(int(data1[h][w][1]) - int(data2[h][w][1]))
if diff > threshold:
diffCount += 1
if diffCount > sensitivity:
break; #break outer loop.
if diffCount > sensitivity:
motionFound = True
else:
data2 = data1
return motionFound
def getFileName(imagePath, imageNamePrefix, currentCount):
rightNow = datetime.datetime.now()
if numberSequence :
filename = imagePath + "/" + imageNamePrefix + str(currentCount) + ".jpg"
else:
filename = "%s/%s%04d%02d%02d-%02d%02d%02d.jpg" % ( imagePath, imageNamePrefix ,rightNow.year, rightNow.month, rightNow.day, rightNow.hour, rightNow.minute, rightNow.second)
return filename
def motionDetection():
print "Scanning for Motion threshold=%i sensitivity=%i ......" % (threshold, sensitivity)
isDay = True
currentCount= 1000
while True:
if scanMotion(testWidth, testHeight, isDay):
filename = getFileName(imagePath, imageNamePrefix, currentCount)
if numberSequence:
currentCount += 1
if isDay:
takeDayImage( imageWidth, imageHeight, filename )
else:
takeNightImage( imageWidth, imageHeight, filename )
if verbose:
print("MotionDetection - Saved %s" % filename)
if ftpOn:
placeFile(filename)
if __name__ == '__main__':
try:
motionDetection()
finally:
print ""
print "+++++++++++++++"
print "Exiting Program"
print "+++++++++++++++"
print ""

Categories