Seite 1 von 1
auf usb Kamera zugreifen und Bild schießen !!
Verfasst: Donnerstag 6. August 2020, 15:16
von Kla-Sur
Hallo,
ist es möglich mit Python auf die Usb-Kamera meines Raspberry
zuzugreifen und ein Bild zu erstellen und das dann per Mail zu versenden!
Mir fehlt eigentlich nur der Zugriff auf die Usb-Kamera !! Unter welchen Stichpunkten kann ich was finden.
Gruß Klaus
Re: auf usb Kamera zugreifen und Bild schießen !!
Verfasst: Donnerstag 6. August 2020, 15:59
von lexx
Mir als Einsteiger fällt da jetzt opencv ein.
Re: auf usb Kamera zugreifen und Bild schießen !!
Verfasst: Freitag 7. August 2020, 09:55
von kaytec
Hallo Kla-Sur,
Code: Alles auswählen
#! /usr/bin/env python
# -*- coding: utf-8
import tkinter as tk
import cv2
from PIL import Image, ImageTk
WIDTH = 640
HEIGHT = 480
DEFAULT_CAM_ID = -1
class USBCam(object):
PROPID_WIDTH = 3
PROPID_HEIGHT = 4
def __init__(self, cam_id = DEFAULT_CAM_ID):
self.cam = cv2.VideoCapture(cam_id)
if not self.cam.isOpened():
raise RuntimeError("can not open camera {0!r}".format(
cam_id))
self.width = int(self.cam.get(self.PROPID_WIDTH))
self.height = int(self.cam.get(self.PROPID_HEIGHT))
@property
def size(self):
return self.width, self.height
def __enter__(self):
return self
def __exit__(self, *args):
self.release()
def get_image(self):
state, frame = self.cam.read()
if not state:
raise RuntimeError("could not read image")
else:
return frame
def release(self):
self.cam.release()
class USBCamUI(tk.Frame):
def __init__(self, parent, usb_cam, width, height,
update_interval = 100):
tk.Frame.__init__(self, parent)
self.parent = parent
self.usb_cam = usb_cam
self.width = width
self.height = height
self.update_interval = update_interval
self.after_id = None
self.tk_image = None
self.image_label = tk.Label(self)
self.image_label.pack()
def run(self):
try:
tk_image = Image.frombytes("RGB", self.usb_cam.size,
self.usb_cam.get_image(), "raw", "BGR").resize(
(self.width, self.height))
except RuntimeError:
self.raise_cam_id_error()
return
self.tk_image = ImageTk.PhotoImage(tk_image)
self.image_label.config(image = self.tk_image)
self.after_id = self.after(self.update_interval, self.run)
def release(self):
self.after_cancel(self.after_id)
self.parent.destroy()
def main():
root = tk.Tk()
root.title("USB CAM")
root.resizable(0, 0)
try:
with USBCam() as usb_cam:
usb_cam_ui = USBCamUI(root, usb_cam, WIDTH, HEIGHT)
usb_cam_ui.pack()
usb_cam_ui.run()
root.protocol("WM_DELETE_WINDOW", usb_cam_ui.release)
root.mainloop()
except RuntimeError:
tk.Label(root, text = "can not open camera {0!r}".format(
DEFAULT_CAM_ID), font = "Arial 20", height = 10).pack()
root.mainloop()
if __name__ == "__main__":
main()
Gruß Frank
Re: auf usb Kamera zugreifen und Bild schießen !!
Verfasst: Montag 10. August 2020, 16:02
von Kla-Sur
Danke Frank !!!
Hoffentlcht komme ich damit klar, als Anfänger. Werde den Code mal probieren. Hast du damit schon Erfahrungen gemacht?
Gruß Klaus
Re: auf usb Kamera zugreifen und Bild schießen !!
Verfasst: Montag 10. August 2020, 16:09
von Kla-Sur
Danke auch Lexx,
dabei handelt es sich doch um ein Programm. Das habe ich nicht gesucht.
Gruß Klaus
Re: auf usb Kamera zugreifen und Bild schießen !!
Verfasst: Montag 10. August 2020, 16:57
von sparrow
@Kla-Sur: Dabei handelt es sich um eine Bibliothek. In dem Code von Kaytec als cv2 importiert.
Re: auf usb Kamera zugreifen und Bild schießen !!
Verfasst: Dienstag 11. August 2020, 08:41
von kaytec
Hallo Kla-Sur,
https://opencv-python-tutroals.readthed ... rials.html
Schaue mal nach "imwrite()".
Gruß Frank
Re: auf usb Kamera zugreifen und Bild schießen !!
Verfasst: Dienstag 11. August 2020, 10:26
von Kla-Sur
Moin, habe es gerade probiert.
Das habe ich als Fehlermeldung erhalten
Python 3.7.3 (/usr/bin/python3)
>>> %Run Mail.py
>>> %Run Bild.py
Traceback (most recent call last):
File "/home/pi/Bild.py", line 5, in <module>
import cv2
ModuleNotFoundError: No module named 'cv2'
Warum kann er die Datei nicht laden ?? import cv2??
Ich das Script Bild.py genannt!!
Wie gesagt ich bin Anfänger.
Gruß Klaus
Re: auf usb Kamera zugreifen und Bild schießen !!
Verfasst: Dienstag 11. August 2020, 10:31
von sparrow
Du möchtest ein Modul verwenden, das nicht Teil der Standard-Bibliothek ist. Also musst du es erst installieren. Das Paket heißt "opencv-python" in pypi.
Re: auf usb Kamera zugreifen und Bild schießen !!
Verfasst: Dienstag 11. August 2020, 18:04
von Kla-Sur
Moin sparrow,
danke für die Hilfe.
Ich muss jetzt das Paket auf den Raspberry Pi laden um an eine Bibiothek zu kommen?
sudo install opencv-python auf dem Raspberry ausführen oder verstehe ich etwas falsch.
Gruß Klaus
Re: auf usb Kamera zugreifen und Bild schießen !!
Verfasst: Dienstag 11. August 2020, 18:05
von Kla-Sur
Moin sparrow,
danke für die Hilfe.
Ich muss jetzt das Paket auf den Raspberry Pi laden um an eine Bibiothek zu kommen?
sudo install opencv-python auf dem Raspberry ausführen oder verstehe ich etwas falsch.
Gruß Klaus
Re: auf usb Kamera zugreifen und Bild schießen !!
Verfasst: Dienstag 11. August 2020, 18:55
von kaytec
Hallo Kla-Sur,
sudo apt install python-opencv --> sollte richtig sein.
oder -->
https://pypi.org/project/opencv-python/
wirft bei mir eine Fehlermeldung ? Habe es bestimmt über apt-install installiert ?!
Code: Alles auswählen
Collecting opencv-python
Using cached https://files.pythonhosted.org/packages/a1/d6/8422797e35f8814b1d9842530566a949d9b5850a466321a6c1d5a99055ee/opencv-python-4.3.0.38.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-3e1f61xv/opencv-python/setup.py", line 9, in <module>
import skbuild
ModuleNotFoundError: No module named 'skbuild'
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-3e1f61xv/opencv-python/
Gruß Frank
Re: auf usb Kamera zugreifen und Bild schießen !!
Verfasst: Mittwoch 12. August 2020, 12:45
von Kla-Sur
Moin,
habe run pip install opencv-python auf dem Raspberry in der Console ausgeführt.
Das war das Resultat:
pip install opencv-python
Looking in indexes:
https://pypi.org/simple,
https://www.piwheels.org/simple
Collecting opencv-python
Downloading
https://files.pythonhosted.org/packages ... .38.tar.gz (88.0MB)
100% |████████████████████████████████| 88.0MB 1.8kB/s
Installing build dependencies ... error
Complete output from command /usr/bin/python -m pip install --ignore-installed --no-user --prefix /tmp/pip-build-env-cusc7f --no-warn-script-location --no-binary :none: --only-binary :none: -i
https://pypi.org/simple --extra-index-url
https://www.piwheels.org/simple -- setuptools wheel scikit-build cmake pip "numpy==1.11.3; python_version=='3.5'" "numpy==1.13.3; python_version=='3.6'" "numpy==1.14.5; python_version=='3.7'" "numpy==1.17.3; python_version>='3.8'":
Ignoring numpy: markers 'python_version == "3.5"' don't match your environment
Ignoring numpy: markers 'python_version == "3.6"' don't match your environment
Ignoring numpy: markers 'python_version == "3.7"' don't match your environment
Ignoring numpy: markers 'python_version >= "3.8"' don't match your environment
Looking in indexes:
https://pypi.org/simple,
https://www.piwheels.org/simple,
https://www.piwheels.org/simple
Collecting setuptools
Downloading
https://files.pythonhosted.org/packages ... ne-any.whl (583kB)
Collecting wheel
Downloading
https://files.pythonhosted.org/packages ... ne-any.whl
Collecting scikit-build
Downloading
https://files.pythonhosted.org/packages ... ne-any.whl (72kB)
Collecting cmake
Downloading
https://files.pythonhosted.org/packages ... 8.0.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-install-R4dwmC/cmake/setup.py", line 7, in <module>ds
from skbuild import setup
ImportError: No module named skbuild
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-R4dwmC/cmake/
----------------------------------------
Command "/usr/bin/python -m pip install --ignore-installed --no-user --prefix /tmp/pip-build-env-cusc7f --no-warn-script-location --no-binary :none: --only-binary :none: -i
https://pypi.org/simple --extra-index-url
https://www.piwheels.org/simple -- setuptools wheel scikit-build cmake pip "numpy==1.11.3; python_version=='3.5'" "numpy==1.13.3; python_version=='3.6'" "numpy==1.14.5; python_version=='3.7'" "numpy==1.17.3; python_version>='3.8'"" failed with error code 1 in None
Habe das Kamera-Script gestartet und wieder der gleiche Fehler beim Import
>> %Run Bild.py
Traceback (most recent call last):
File "/home/pi/Bild.py", line 5, in <module>
import cv2
ModuleNotFoundError: No module named 'cv2'
Bin gerade dabei die Fehler zu lesen!!!
Aber jetzt schon mal die Frage ? Woran kann es liegen ?
Gruß Klaus
Re: auf usb Kamera zugreifen und Bild schießen !!
Verfasst: Mittwoch 12. August 2020, 13:19
von sparrow
Was ins Auge sticht: Du verwendest kein Python 3.5 bis 3.8. Welche Version verwendest du denn?
Re: auf usb Kamera zugreifen und Bild schießen !!
Verfasst: Donnerstag 13. August 2020, 07:38
von Kla-Sur
Moin, habe die Python-Version geprüft. 2.7. Kann mir einer helfen beim Update auf die neue Version3.8.5?
Es handelt sich um einen RaspberryPi;4B;2GB und das betriebssytem bust-debian.
Habe schon folgenden Befehl probiert sudo apt-get remove python ! sudo apt-get install python, aber
das geht irendwie nicht.
Habt Geduld mit mir.
Gruß Klaus
Re: auf usb Kamera zugreifen und Bild schießen !!
Verfasst: Donnerstag 13. August 2020, 07:44
von __blackjack__
@Kla-Sur: Erst mal schauen ob ein Python 3 nicht bereits installiert ist. ``python`` ist Python 2, ``python3`` ist Python 3. Die können deswegen problemlos beide installiert sein.
Re: auf usb Kamera zugreifen und Bild schießen !!
Verfasst: Donnerstag 13. August 2020, 12:15
von hyle
@Kla-Sur Ergänzend noch ein paar Hinweise. Wenn es sich um ein Raspbian oder neuerdings Raspberry Pi OS handelt dann ist Python 3 neben Python 2 schon vorinstalliert.
Noch etwas zur Installation von Modulen. Diese müssen für die jeweilige Python-Version installiert sein:
"pip install ..." (Python 2)
"pip3 install ..." (Python 3)
und evtl. noch zum Aufruf von Skripten:
"python /Pfad/zum/Sript.py" (mit Python 2)
"python3 /Pfad/zum/Sript.py" (mit Python 3)
Re: auf usb Kamera zugreifen und Bild schießen !!
Verfasst: Donnerstag 13. August 2020, 12:54
von kaytec
Hallo Kla-Su,
oder --> sudo apt install python3
Gru? Frank