Flux ascii

Fermé
emhan - Modifié le 28 avril 2021 à 20:38
yg_be Messages postés 22722 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 25 avril 2024 - 29 avril 2021 à 10:01
Bonjour souhaite recupérer le flux de se programme


from cv2 import cv2
import numpy as np

def main():
  vc = cv2.VideoCapture(0,cv2.CAP_DSHOW)

  if vc.isOpened():
    rval, frame = vc.read()
  else:
    rval = False

  while rval:
    rval, frame = vc.read()
    print(toASCII(frame))

    key = cv2.waitKey(50) # 50ms pause -> ~20fps
    # Press echap to end
    if key == 27:
      break

def toASCII(frame, cols = 120, rows = 35):

  frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  height, width = frame.shape
  cell_width = width / cols
  cell_height = height / rows
  if cols > width or rows > height:
    raise ValueError('Too many cols or rows.')
  result = ""
  for i in range(rows):
    for j in range(cols):
      gray = np.mean(
        frame[int(i * cell_height):min(int((i + 1) * cell_height), height), int(j * cell_width):min(int((j + 1) * cell_width), width)]
      )
      result += grayToChar(gray)
    result += '\n'
  return result

def grayToChar(gray):
  CHAR_LIST = ' .:-=+*#%@'
  num_chars = len(CHAR_LIST)
  return CHAR_LIST[
    min(
      int(gray * num_chars / 255),
      num_chars - 1
    )
  ]
  
if __name__ == '__main__':
    main()



pour en faire une camera virtuel sous obs mais je sait pas comment m'y prendre merci d'avance.

EDIT : Ajout des balises de code (la coloration syntaxique).
Explications disponibles ici : ICI

Merci d'y penser dans tes prochains messages.

1 réponse

yg_be Messages postés 22722 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 25 avril 2024 1 476
29 avril 2021 à 10:01
bonjour,
as-tu déjà fait du python sous obs?
sinon, commence peut-être par un exemple simple, pour comprendre le principe:
https://gist.github.com/nibalizer/a6649abee758da3f8d08ef5e164b524c
1