• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

basic html/python coding help needed (Raspberry Pi)

Joined
Nov 1, 2008
Messages
4,213 (0.75/day)
Location
Vietnam
System Name Gaming System / HTPC-Server
Processor i7 8700K (@4.8 Ghz All-Core) / R7 5900X
Motherboard Z370 Aorus Ultra Gaming / MSI B450 Mortar Max
Cooling CM ML360 / CM ML240L
Memory 16Gb Hynix @3200 MHz / 16Gb Hynix @3000Mhz
Video Card(s) Zotac 3080 / Colorful 1060
Storage 750G MX300 + 2x500G NVMe / 40Tb Reds + 1Tb WD Blue NVMe
Display(s) LG 27GN800-B 27'' 2K 144Hz / Sony TV
Case Xigmatek Aquarius Plus / Corsair Air 240
Audio Device(s) On Board Realtek
Power Supply Super Flower Leadex III Gold 750W / Andyson TX-700 Platinum
Mouse Logitech G502 Hero / K400+
Keyboard Wooting Two / K400+
Software Windows 10 x64
Benchmark Scores Cinebench R15 = 1542 3D Mark Timespy = 9758
Hi all,

I was wondering whether anyone could help?

I'm trying to create a .html file that will cycle between different images after x amount of seconds.

I want to do this as I have a Raspberry set up with multiple cameras and want a basic cycle through them all.

The screen I have is small (netbook sized) and so displaying the two feeds side-by-side is not very helpful.

If it's not possible in html, maybe something based in python would work?

If found some info here, but all the examples have side-by-side feeds :(

http://www.lavrsen.dk/foswiki/bin/v..._see_more_than_one_camera_stream_at_a_time_63

Thanks for any help and apologies
 

silentbogo

Moderator
Staff member
Joined
Nov 20, 2013
Messages
5,474 (1.44/day)
Location
Kyiv, Ukraine
System Name WS#1337
Processor Ryzen 7 3800X
Motherboard ASUS X570-PLUS TUF Gaming
Cooling Xigmatek Scylla 240mm AIO
Memory 4x8GB Samsung DDR4 ECC UDIMM
Video Card(s) Inno3D RTX 3070 Ti iChill
Storage ADATA Legend 2TB + ADATA SX8200 Pro 1TB
Display(s) Samsung U24E590D (4K/UHD)
Case ghetto CM Cosmos RC-1000
Audio Device(s) ALC1220
Power Supply SeaSonic SSR-550FX (80+ GOLD)
Mouse Logitech G603
Keyboard Modecom Volcano Blade (Kailh choc LP)
VR HMD Google dreamview headset(aka fancy cardboard)
Software Windows 11, Ubuntu 20.04 LTS
With some HTML and Javascript you can do it quite easily.

BTW, the example you've listed is for multiple IP cameras, so if you have a two physical webcams or a camera on CSI interface(e.g. PiCamera), you will need additional script to save snapshots to image files periodically.

Tell me what's your setup, and I'll try to put something together, or at least give you some directions.
 
Joined
Nov 1, 2008
Messages
4,213 (0.75/day)
Location
Vietnam
System Name Gaming System / HTPC-Server
Processor i7 8700K (@4.8 Ghz All-Core) / R7 5900X
Motherboard Z370 Aorus Ultra Gaming / MSI B450 Mortar Max
Cooling CM ML360 / CM ML240L
Memory 16Gb Hynix @3200 MHz / 16Gb Hynix @3000Mhz
Video Card(s) Zotac 3080 / Colorful 1060
Storage 750G MX300 + 2x500G NVMe / 40Tb Reds + 1Tb WD Blue NVMe
Display(s) LG 27GN800-B 27'' 2K 144Hz / Sony TV
Case Xigmatek Aquarius Plus / Corsair Air 240
Audio Device(s) On Board Realtek
Power Supply Super Flower Leadex III Gold 750W / Andyson TX-700 Platinum
Mouse Logitech G502 Hero / K400+
Keyboard Wooting Two / K400+
Software Windows 10 x64
Benchmark Scores Cinebench R15 = 1542 3D Mark Timespy = 9758
With some HTML and Javascript you can do it quite easily.

BTW, the example you've listed is for multiple IP cameras, so if you have a two physical webcams or a camera on CSI interface(e.g. PiCamera), you will need additional script to save snapshots to image files periodically.

Tell me what's your setup, and I'll try to put something together, or at least give you some directions.

It's a PiCam + a webcam used on the front door to my apartment. The PiCam is outwards facing to see a persons face, if they are at the door and the webcam is in a corner above, giving a wide-angle view of the hallway and door.

I am using motion for the streaming and capture. It works well, though a few more snapshots than I'd like.

I have both streams working at the moment, they are just streaming to 2 different ports (same ip). I'd really like to have a round-robin type setup, if possible.
 
Last edited:

silentbogo

Moderator
Staff member
Joined
Nov 20, 2013
Messages
5,474 (1.44/day)
Location
Kyiv, Ukraine
System Name WS#1337
Processor Ryzen 7 3800X
Motherboard ASUS X570-PLUS TUF Gaming
Cooling Xigmatek Scylla 240mm AIO
Memory 4x8GB Samsung DDR4 ECC UDIMM
Video Card(s) Inno3D RTX 3070 Ti iChill
Storage ADATA Legend 2TB + ADATA SX8200 Pro 1TB
Display(s) Samsung U24E590D (4K/UHD)
Case ghetto CM Cosmos RC-1000
Audio Device(s) ALC1220
Power Supply SeaSonic SSR-550FX (80+ GOLD)
Mouse Logitech G603
Keyboard Modecom Volcano Blade (Kailh choc LP)
VR HMD Google dreamview headset(aka fancy cardboard)
Software Windows 11, Ubuntu 20.04 LTS
Ok, so if you haven't set up image capture from your cameras, we can start with that.
I found this article for PiCam w/ small script in Python:
https://www.raspberrypi.org/forums/viewtopic.php?t=117868&p=801443

It's meant to be triggered by motion detector, but if you remove the condition and add a delay - it will continuously record images.
If you don't want to save all images chronologically, and only care about what's happening right now, simply remove the timestamp from the file name (this will also help with streaming).

Second part - webcam.
All you need is install fswebcam tool and you can either write a separate script to capture webcam images, or for the sake of synchronization add a call into your Python script and save it to the other file.

So, your whole script should look like this (I'm just making minimal changes, cause I'm lazy and my Python is a bit rusty):
Code:
import os
import time
import RPi.GPIO as GPIO  # no need if you are not using motion sensors
import Picamera
from subprocess import call # We need this for fswebcam calls

cam = picamera.Picamera()

GPIO.setmode (GPIO.BCM)

pir_pin = 4

GPIO.setup (pir_pin, GPIO.IN) # Sensor pin
os.chdir("/home/yourname/capture/") # switch to capture directory

While True:
if GPIO.input (pir_pin):
print ("MOTION DETECTED")
cam.start_preview ()
cam.capture ("Picam.jpg")
cam.stop_preview ()
# Now let's capture webcam
call (["fswebcam", "Webcam.jpg"])
print("Images captured")
time.sleep (5)

else:
time.sleep (0.2) # Delay, if nothing happened

I'll put together some HTML/JS in a minute or two,.
 
Joined
Jun 26, 2017
Messages
90 (0.04/day)
Location
Germany
Processor Core i5-6500
Motherboard MSI Z170A Krait Gaming
Memory 2x8GB Kingston HyperX Fury Black
Video Card(s) Radeon HD7970
Display(s) 2x BenQ E2200HD
Case Xigmatek Utgard
Power Supply OCZ Fatal1ty 550W
Mouse Mad Catz M.M.O.TE
Keyboard Coolermaster Quickfire TK MX Blue
Software Windows 10 64bit
If you can already get still images from the cams and don't need to save them, a simple HTML Frameset would work.
 
Joined
Nov 1, 2008
Messages
4,213 (0.75/day)
Location
Vietnam
System Name Gaming System / HTPC-Server
Processor i7 8700K (@4.8 Ghz All-Core) / R7 5900X
Motherboard Z370 Aorus Ultra Gaming / MSI B450 Mortar Max
Cooling CM ML360 / CM ML240L
Memory 16Gb Hynix @3200 MHz / 16Gb Hynix @3000Mhz
Video Card(s) Zotac 3080 / Colorful 1060
Storage 750G MX300 + 2x500G NVMe / 40Tb Reds + 1Tb WD Blue NVMe
Display(s) LG 27GN800-B 27'' 2K 144Hz / Sony TV
Case Xigmatek Aquarius Plus / Corsair Air 240
Audio Device(s) On Board Realtek
Power Supply Super Flower Leadex III Gold 750W / Andyson TX-700 Platinum
Mouse Logitech G502 Hero / K400+
Keyboard Wooting Two / K400+
Software Windows 10 x64
Benchmark Scores Cinebench R15 = 1542 3D Mark Timespy = 9758
Ok, so if you haven't set up image capture from your cameras, we can start with that.
I found this article for PiCam w/ small script in Python:
https://www.raspberrypi.org/forums/viewtopic.php?t=117868&p=801443

It's meant to be triggered by motion detector, but if you remove the condition and add a delay - it will continuously record images.
If you don't want to save all images chronologically, and only care about what's happening right now, simply remove the timestamp from the file name (this will also help with streaming).

Second part - webcam.
All you need is install fswebcam tool and you can either write a separate script to capture webcam images, or for the sake of synchronization add a call into your Python script and save it to the other file.

So, your whole script should look like this (I'm just making minimal changes, cause I'm lazy and my Python is a bit rusty):
Code:
import os
import time
import RPi.GPIO as GPIO  # no need if you are not using motion sensors
import Picamera
from subprocess import call # We need this for fswebcam calls

cam = picamera.Picamera()

GPIO.setmode (GPIO.BCM)

pir_pin = 4

GPIO.setup (pir_pin, GPIO.IN) # Sensor pin
os.chdir("/home/yourname/capture/") # switch to capture directory

While True:
if GPIO.input (pir_pin):
print ("MOTION DETECTED")
cam.start_preview ()
cam.capture ("Picam.jpg")
cam.stop_preview ()
# Now let's capture webcam
call (["fswebcam", "Webcam.jpg"])
print("Images captured")
time.sleep (5)

else:
time.sleep (0.2) # Delay, if nothing happened

I'll put together some HTML/JS in a minute or two,.

Thanks. I've already got all of that set up through motion.

What I really want to do is cycle through the different streams (one at 192.168.0.5:8081 and the other is at 192.168.0.5:8082)

I've tried the xdotool to cycle through chromium tabs, but that isn't working :(
 

silentbogo

Moderator
Staff member
Joined
Nov 20, 2013
Messages
5,474 (1.44/day)
Location
Kyiv, Ukraine
System Name WS#1337
Processor Ryzen 7 3800X
Motherboard ASUS X570-PLUS TUF Gaming
Cooling Xigmatek Scylla 240mm AIO
Memory 4x8GB Samsung DDR4 ECC UDIMM
Video Card(s) Inno3D RTX 3070 Ti iChill
Storage ADATA Legend 2TB + ADATA SX8200 Pro 1TB
Display(s) Samsung U24E590D (4K/UHD)
Case ghetto CM Cosmos RC-1000
Audio Device(s) ALC1220
Power Supply SeaSonic SSR-550FX (80+ GOLD)
Mouse Logitech G603
Keyboard Modecom Volcano Blade (Kailh choc LP)
VR HMD Google dreamview headset(aka fancy cardboard)
Software Windows 11, Ubuntu 20.04 LTS
So, you already have both streams set up over IP?
What type of streams are you using and what streaming software?

If it's gstreamer or any other type of rtsp server, then you might not need a webpage at all, just use multiple VLC instances side-by-side on a client PC, or get a free and/or opensource security camera monitoring software (most do support RTSP protocol).
 
Joined
Nov 1, 2008
Messages
4,213 (0.75/day)
Location
Vietnam
System Name Gaming System / HTPC-Server
Processor i7 8700K (@4.8 Ghz All-Core) / R7 5900X
Motherboard Z370 Aorus Ultra Gaming / MSI B450 Mortar Max
Cooling CM ML360 / CM ML240L
Memory 16Gb Hynix @3200 MHz / 16Gb Hynix @3000Mhz
Video Card(s) Zotac 3080 / Colorful 1060
Storage 750G MX300 + 2x500G NVMe / 40Tb Reds + 1Tb WD Blue NVMe
Display(s) LG 27GN800-B 27'' 2K 144Hz / Sony TV
Case Xigmatek Aquarius Plus / Corsair Air 240
Audio Device(s) On Board Realtek
Power Supply Super Flower Leadex III Gold 750W / Andyson TX-700 Platinum
Mouse Logitech G502 Hero / K400+
Keyboard Wooting Two / K400+
Software Windows 10 x64
Benchmark Scores Cinebench R15 = 1542 3D Mark Timespy = 9758
So, you already have both streams set up over IP?
What type of streams are you using and what streaming software?

If it's gstreamer or any other type of rtsp server, then you might not need a webpage at all, just use multiple VLC instances side-by-side on a client PC, or get a free and/or opensource security camera monitoring software (most do support RTSP protocol).

I'm using motion with raspbian. images are captured on the device and the streams are good :)

It's streaming to an LCD inside a picture frame that i have set up as a 'kiosk', so no kb/mouse inputs. I just want it to boot, open the browser and cycle between the two streams with no user intervention.

It's basically a ghetto video intercom system (once i get a couple more echo dots)

If there's an OS/Software solution that will do this, I'm more than open to suggestions.
 

silentbogo

Moderator
Staff member
Joined
Nov 20, 2013
Messages
5,474 (1.44/day)
Location
Kyiv, Ukraine
System Name WS#1337
Processor Ryzen 7 3800X
Motherboard ASUS X570-PLUS TUF Gaming
Cooling Xigmatek Scylla 240mm AIO
Memory 4x8GB Samsung DDR4 ECC UDIMM
Video Card(s) Inno3D RTX 3070 Ti iChill
Storage ADATA Legend 2TB + ADATA SX8200 Pro 1TB
Display(s) Samsung U24E590D (4K/UHD)
Case ghetto CM Cosmos RC-1000
Audio Device(s) ALC1220
Power Supply SeaSonic SSR-550FX (80+ GOLD)
Mouse Logitech G603
Keyboard Modecom Volcano Blade (Kailh choc LP)
VR HMD Google dreamview headset(aka fancy cardboard)
Software Windows 11, Ubuntu 20.04 LTS
I'm using motion with raspbian. images are captured on the device and the streams are good :)

It's streaming to an LCD inside a picture frame that i have set up as a 'kiosk', so no kb/mouse inputs. I just want it to boot, open the browser and cycle between the two streams with no user intervention.

It's basically a ghetto video intercom system (once i get a couple more echo dots)

If there's an OS/Software solution that will do this, I'm more than open to suggestions.
video.php in motion does not contain any PHP code, so you can either edit that file on your host, or add another HTML/PHP file with whatever setup you need.

For example, if you want to switch between cameras once a second, all you need to do is replace the contents of video.php with following:

Code:
<html>
  <head><title>My IPcam manager</title></head>
  <body>
    <!-- Change the image size to whatever works better on your screen -->
    <img id="display" src="http://192.168.0.5:8081" width="640" height="480"/>
      <script>
        var camList = ["http://192.168.0.5:8081", "http://192.168.0.5:8082"];
        var camDisplay = documet.getElementById("display");
        var pos=0;
        function switchCamera(){
          if(++pos>camList.length) pos=0; // Reset camera ID when reached the end of list
          camDisplay.src = camList[pos];
        }
        setInterval("switchCamera", 1000); // Call camera switch once a second
      </script>
    </body>
</html>

That's a very-super-uber-basic slideshow code, which is usually included in JavaScript tutorials for beginners.
Try and see if it works.
 
Joined
Nov 1, 2008
Messages
4,213 (0.75/day)
Location
Vietnam
System Name Gaming System / HTPC-Server
Processor i7 8700K (@4.8 Ghz All-Core) / R7 5900X
Motherboard Z370 Aorus Ultra Gaming / MSI B450 Mortar Max
Cooling CM ML360 / CM ML240L
Memory 16Gb Hynix @3200 MHz / 16Gb Hynix @3000Mhz
Video Card(s) Zotac 3080 / Colorful 1060
Storage 750G MX300 + 2x500G NVMe / 40Tb Reds + 1Tb WD Blue NVMe
Display(s) LG 27GN800-B 27'' 2K 144Hz / Sony TV
Case Xigmatek Aquarius Plus / Corsair Air 240
Audio Device(s) On Board Realtek
Power Supply Super Flower Leadex III Gold 750W / Andyson TX-700 Platinum
Mouse Logitech G502 Hero / K400+
Keyboard Wooting Two / K400+
Software Windows 10 x64
Benchmark Scores Cinebench R15 = 1542 3D Mark Timespy = 9758
video.php in motion does not contain any PHP code, so you can either edit that file on your host, or add another HTML/PHP file with whatever setup you need.

For example, if you want to switch between cameras once a second, all you need to do is replace the contents of video.php with following:

Code:
<html>
  <head><title>My IPcam manager</title></head>
  <body>
    <!-- Change the image size to whatever works better on your screen -->
    <img id="display" src="http://192.168.0.5:8081" width="640" height="480"/>
      <script>
        var camList = ["http://192.168.0.5:8081", "http://192.168.0.5:8082"];
        var camDisplay = documet.getElementById("display");
        var pos=0;
        function switchCamera(){
          if(++pos>camList.length) pos=0; // Reset camera ID when reached the end of list
          camDisplay.src = camList[pos];
        }
        setInterval("switchCamera", 1000); // Call camera switch once a second
      </script>
    </body>
</html>

That's a very-super-uber-basic slideshow code, which is usually included in JavaScript tutorials for beginners.
Try and see if it works.

Unfortunately, it doesn't work :( It only shows the first camera when testing on my desktop.

Once I've got the 2 camera switching working, I'd like to also add a local .html file to the cycle. That way, I can get it to cycle between local .html (2 cameras side-by-side) - camera 1 - camera 2
 

silentbogo

Moderator
Staff member
Joined
Nov 20, 2013
Messages
5,474 (1.44/day)
Location
Kyiv, Ukraine
System Name WS#1337
Processor Ryzen 7 3800X
Motherboard ASUS X570-PLUS TUF Gaming
Cooling Xigmatek Scylla 240mm AIO
Memory 4x8GB Samsung DDR4 ECC UDIMM
Video Card(s) Inno3D RTX 3070 Ti iChill
Storage ADATA Legend 2TB + ADATA SX8200 Pro 1TB
Display(s) Samsung U24E590D (4K/UHD)
Case ghetto CM Cosmos RC-1000
Audio Device(s) ALC1220
Power Supply SeaSonic SSR-550FX (80+ GOLD)
Mouse Logitech G603
Keyboard Modecom Volcano Blade (Kailh choc LP)
VR HMD Google dreamview headset(aka fancy cardboard)
Software Windows 11, Ubuntu 20.04 LTS
My bad. I've made a few grammatical errors, like missing "n" in the "document", and passing function name in quotes.

Try now.

Code:
<html>
  <head><title>&#x1f640; My IPcam Uber manager v0.2 pre-pre-pre-alpha (available for preorder soon)</title></head>
  <body>
    <!-- Change the image size to whatever works better on your screen -->
    <img id="display" src="http://192.168.0.5:8081" width="640" height="480"/>
      <script>
        var camList = ["http://192.168.0.5:8081", "http://192.168.0.5:8081"];
        var camDisplay = document.getElementById("display");
        var pos=0;
        function switchCamera(){
          if(++pos>camList.length-1) pos=0; // Reset camera ID when reached the end of list
          camDisplay.src = camList[pos];
        }
        setInterval(switchCamera, 1000); // Call camera switch once a second
      </script>
    </body>
</html>
 
Joined
Nov 1, 2008
Messages
4,213 (0.75/day)
Location
Vietnam
System Name Gaming System / HTPC-Server
Processor i7 8700K (@4.8 Ghz All-Core) / R7 5900X
Motherboard Z370 Aorus Ultra Gaming / MSI B450 Mortar Max
Cooling CM ML360 / CM ML240L
Memory 16Gb Hynix @3200 MHz / 16Gb Hynix @3000Mhz
Video Card(s) Zotac 3080 / Colorful 1060
Storage 750G MX300 + 2x500G NVMe / 40Tb Reds + 1Tb WD Blue NVMe
Display(s) LG 27GN800-B 27'' 2K 144Hz / Sony TV
Case Xigmatek Aquarius Plus / Corsair Air 240
Audio Device(s) On Board Realtek
Power Supply Super Flower Leadex III Gold 750W / Andyson TX-700 Platinum
Mouse Logitech G502 Hero / K400+
Keyboard Wooting Two / K400+
Software Windows 10 x64
Benchmark Scores Cinebench R15 = 1542 3D Mark Timespy = 9758
Still doesn't cycle :(

Can it be tested by putting in different URLs for each camera, right? e.g. http://goo.gl for the first one and http://yahoo.co.uk for the second and the script should still switch the pages?
 
Top