web-server cam live streaming Using RPi

DEPENDENCY:  

  • Before installation Adafruit IO first check all the dependency are readily installed or not!!
  • If not go through following commands just copy and paste in RPi terminal

PYTHON 2:


sudo apt-get update
sudo apt-get install python-pip
sudo python -m pip install --upgrade pip setuptools wheel

PYTHON 3:


sudo apt-get update
sudo apt-get install python3-pip
sudo python3 -m pip install --upgrade pip setuptools wheel

INSTALL PICAMERA LIBRARY

PYTHON 2:


sudo apt-get install python-picamera

PYTHON 3:


sudo apt-get install python3-picamera

ENABLING CAMERA IN RASPBERRY PI

METHODE_1: GRAPHICAL METHOD

METHOD _2: USING COMMAND PROMPT

Open Terminal paste below code


sudo raspi-config

Follow the below steps for enabling Camera in RPi 

Setp_1: Choose “Interfacing Options”

Setp_2: Click on “”

 Setp_3: Then Click on “”

Setp_4: Click on “”

CONNECTION:

CODE:

CAPTURE PHOTO BY PYTHON CODE


#Tested Code by Robogenesis
import picamera
import time
camera = picamera.PiCamera()
camera.vflip = True # for rotate camera
camera.brightness= 60 # Set picam Brightness
camera.annotate_text='Looking Great'
camera.resolution = (1080,960)
camera.resolution = "HD"
#it support .jpg,. jpeg, .png, .bmp, .gif etc... 
camera.capture('cam_features.jpeg')#it will save in current directory  
#camera.capture('/home/pi/cam_features.jpeg')# it wil save where u want to save  

CODE FOR VIDEO LIVE STREAMING


#Tested Code by Robogenesis 
import picamera
import time

camera = picamera.PiCamera()

camera.brightness = 60
camera.resolution = 'HD'
#camera.vflip = True #flip camera Vertically 
#camera.hflip = True #Flip camera horizontally

#For live video Streaming 
camera.start_preview()#start video stream
time.sleep(8)
camera.stop_preview()#stop video stream

CODE VIDEO RECORDING


#Tested Code by Robogenesis
import picamera
import time

camera = picamera.PiCamera()

camera.brightness = 60
camera.resolution = 'HD'
#camera.vflip = True #flip camera Vertically 
#camera.hflip = True #Flip camera horizontally'

#video Recording
print("recording start")
camera.start_recording("/home/pi/picamrec.h264")
time.sleep(7)
camera.stop_recording()
camera.close()
print("recording stop")

TO PLAY RECORD VIDEO

  • Open Terminal
  • go to directory where video saved; In my case video saved  in “pi” Directory.

omxplayer picamrec.h264

Web-server PiCam

CODE FOR WEBSERVER PICAM


# Web streaming example
# Source code from the official PiCamera package
# http://picamera.readthedocs.io/en/latest/recipes2.html#web-streaming

import io
import picamera
import logging
import socketserver
from threading import Condition
from http import server

PAGE="""\

Raspberry Pi – Surveillance Camera

Raspberry Pi - Surveillance Camera

""" class StreamingOutput(object): def __init__(self): self.frame = None self.buffer = io.BytesIO() self.condition = Condition() def write(self, buf): if buf.startswith(b'\xff\xd8'): # New frame, copy the existing buffer's content and notify all # clients it's available self.buffer.truncate() with self.condition: self.frame = self.buffer.getvalue() self.condition.notify_all() self.buffer.seek(0) return self.buffer.write(buf) class StreamingHandler(server.BaseHTTPRequestHandler): def do_GET(self): if self.path == '/': self.send_response(301) self.send_header('Location', '/index.html') self.end_headers() elif self.path == '/index.html': content = PAGE.encode('utf-8') self.send_response(200) self.send_header('Content-Type', 'text/html') self.send_header('Content-Length', len(content)) self.end_headers() self.wfile.write(content) elif self.path == '/stream.mjpg': self.send_response(200) self.send_header('Age', 0) self.send_header('Cache-Control', 'no-cache, private') self.send_header('Pragma', 'no-cache') self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME') self.end_headers() try: while True: with output.condition: output.condition.wait() frame = output.frame self.wfile.write(b'--FRAME\r\n') self.send_header('Content-Type', 'image/jpeg') self.send_header('Content-Length', len(frame)) self.end_headers() self.wfile.write(frame) self.wfile.write(b'\r\n') except Exception as e: logging.warning( 'Removed streaming client %s: %s', self.client_address, str(e)) else: self.send_error(404) self.end_headers() class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer): allow_reuse_address = True daemon_threads = True with picamera.PiCamera(resolution='640x480', framerate=24) as camera: output = StreamingOutput() #Uncomment the next line to change your Pi's Camera rotation (in degrees) #camera.rotation = 90 camera.start_recording(output, format='mjpeg') try: address = ('', 8000) server = StreamingServer(address, StreamingHandler) server.serve_forever() finally: camera.stop_recording()


HOW TO FIND RPI IP ADDRESS


Ifconfig

Open Terminal paste above code and follow below go got ip address of Raspberry Pi

Note:

  • Open Browser of computer or mobile which connected to same network
  • Open new tab and search :8000 ; in my case 192.168.43.75:8000

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Robogenesis

Start your journey with us.

I hope this message finds you well. I’m writing to inquire about your offerings in IoT, robotics, and software solutions courses. Could you please provide details regarding the curriculum, duration, and enrollment process? Thank you in advance for your assistance.

Inquiry Now