Recent

Author Topic: Is it possible to Capture video and modify it before display in Windows (SOLVED)  (Read 7269 times)

af0815

  • Hero Member
  • *****
  • Posts: 1379
This Code was only a small and old testrun in Lazarus for one or two cameras. There are a lot of good information about DirectShow and Pascal in the internet. If you want to dive deeper.

BTW. i have also used gstreamer an windows. But DS was handier.
regards
Andreas

Dzandaa

  • Sr. Member
  • ****
  • Posts: 391
  • From C# to Lazarus
Hi,
This is a skeleton for a Frame Grabber using DirectX9.

Feel free to correct and improve it.

I don't know how to add some drawings on the Frame ( like lines passing at the center)

and I'm not sure that the correct format is selected.

To be continued.

B->

Regards,
Dzandaa

af0815

  • Hero Member
  • *****
  • Posts: 1379
I don't know how to add some drawings on the Frame ( like lines passing at the center)
So it is better you save the image direct and not to load in a visual component. So you can do postprozessing
Code: Pascal  [Select][+][-]
  1. procedure TTst_FormVideoTmp.SnapShot(var bmp:TBitmap);
  2. begin
  3.   if FilterGraph.Active then begin
  4.     SampleGrabber.GetBitmap(bmp);  
  5.   end
  6. end;
  7.  
Here a trigger a snapshot and store it in a Bitmap. This bitmap can i postprocess and show the result the user. I do it in my apps for insert information and store the bitmap on the filesystem.   
regards
Andreas

Dzandaa

  • Sr. Member
  • ****
  • Posts: 391
  • From C# to Lazarus
Hi,

Thank you again Andreas.

In this version, I Grab Frames in a TBitmap with a Timer, add Cyan Square at 10, 10 30, 30 and display it in a TImage.

For the moment, I use only the first camera (just what I need), but some code must be added when you change the camera number.

I'm sure there are other bugs to correct, but it's a start.

If you change the resolution, you have to stop and start the camera again.

@wadu, try this code and tell me if it works? I only tested it on a Win7 pro 64 bits.


B->
Regards,
Dzandaa

af0815

  • Hero Member
  • *****
  • Posts: 1379
With GraphStudioNext you can see, what parameters you can change if the camera is running. Camera, resolution, mediatypes or pins can not changed in run mode. Other parameter (eg. brigthness,...) can well queried and changed in run mode. And each camera can have another set of parameters. :-)

Some hints from my bookmarks:
A good starting point is https://tutoriels.pecaudchristopher.com/tutoriels_windows/espace_delphi/TutorielDSPACK234Delphi.php and this can be good translated to your language, if you are not speaking french.

https://learn.microsoft.com/de-de/windows/win32/directshow/directshow-system-overview?redirectedfrom=MSDN
https://stackoverflow.com/questions/tagged/dspack

BTW: Actual i use Linux and V4L with Lazarus. So i can also use special cameras, with no standard video or resolution like 20 mpx and more.

« Last Edit: June 14, 2024, 09:39:57 pm by af0815 »
regards
Andreas

Dzandaa

  • Sr. Member
  • ****
  • Posts: 391
  • From C# to Lazarus
Hi,

@Andreas, thank you for the links.

I also use Linux with 5DPO and V4L, it's easier,
I just wanted to know if it was possible to do the same thing in Windows to integrate it into my 3D Scanner, Windows version.

I can select the camera now :)

Too bad you can't easily use BGRABitmap in
Code: Pascal  [Select][+][-]
  1. SampleGrabber.GetBitmap(Picture)
instead of TBitmap.

But it's a start.

Looking now how to change Brightness, etc...

Thank you.

B->
Regards,
Dzandaa

Dzandaa

  • Sr. Member
  • ****
  • Posts: 391
  • From C# to Lazarus
Hi,

The program work fine in Windows 7 64bits Pro
But Crashes in Windows 10 when I try to enumerate the camera formats
with and "invalid typecast":

Code: Pascal  [Select][+][-]
  1. procedure TVideoGrabberDX9Form.FillFormats(CameraNum: Integer);
  2. var
  3.         PinList: TPinList;
  4.         Cnt: Integer;
  5.         MediaStr : String;
  6. begin
  7.         if(VideoMediaTypes <> nil) then VideoMediaTypes.Free;
  8.         VideoMediaTypes := TEnumMediaType.Create;
  9.         try
  10.                 VideoDev.SelectGUIDCategory(CLSID_VideoInputDeviceCategory);
  11.                 Filter.BaseFilter.Moniker := VideoDev.GetMoniker(CameraNum);
  12.                 Filter.FilterGraph := FilterGraph;
  13.                 FilterGraph.Active := true;
  14.                 PinList := TPinList.Create(Filter as IBaseFilter); // Here error: "Invalid typecast"
  15.                 VideoMediaTypes.Assign(PinList.First);
  16.                 CBVideoFormats.Clear;
  17.                 for Cnt := 0 to Pred(VideoMediaTypes.Count) do
  18.                 begin
  19.                         MediaStr := VideoMediaTypes.MediaDescription[Cnt];
  20.                         CBVideoFormats.Items.Add(MediaStr);
  21.                 end;
  22.                 if(CBVideoFormats.Items.Count <> 0) then CBVideoFormats.ItemIndex := 0;
  23.         finally
  24.                 FreeAndNil(PinList);
  25.         end;
  26. end;  
  27.  

Any idea?

Thank you.

B->
Regards,
Dzandaa

af0815

  • Hero Member
  • *****
  • Posts: 1379
Insert a step
Code: Pascal  [Select][+][-]
  1. var
  2.   MyBF : IBaseFilter;
  3.  
  4. .....
  5.  
  6.       MyBF:= Filter as IBaseFilter;
  7.       PinList := TPinList.Create(MyBF);
  8. ...
  9.  
look working here with win10, but only with the extra step.


regards
Andreas

Dzandaa

  • Sr. Member
  • ****
  • Posts: 391
  • From C# to Lazarus
Hi,

Thank you.

Same error at:

Code: Pascal  [Select][+][-]
  1. MyBF = Filter as IBaseFilter;
Invalid typecast

Lazarus 2.2.4
FPC 3.2.2

I HATE Windows !!!

B->

Regards,
Dzandaa

jamie

  • Hero Member
  • *****
  • Posts: 6735
Code: Pascal  [Select][+][-]
  1. :=  not =  
  2.  

See the quote difference?

Maybe I missed something, too?

The only true wisdom is knowing you know nothing

Dzandaa

  • Sr. Member
  • ****
  • Posts: 391
  • From C# to Lazarus
Hi,
@jamie:
Quote
:=  not = 

The typo was just on the forum, not in the code:

Code: Pascal  [Select][+][-]
  1. // *****************************
  2. // ***** Get Video Formats *****
  3. // *****************************
  4. procedure TVideoGrabberDX9Form.FillFormats(CameraNum: Integer);
  5. var
  6.  PinList: TPinList;
  7.  Cnt: Integer;
  8.  MediaStr : String;
  9.  IBF: IBaseFilter;
  10. begin
  11.  if(VideoMediaTypes <> nil) then VideoMediaTypes.Free;
  12.  VideoMediaTypes := TEnumMediaType.Create;
  13.  try
  14.   VideoDev.SelectGUIDCategory(CLSID_VideoInputDeviceCategory);
  15.   Filter.BaseFilter.Moniker := VideoDev.GetMoniker(CameraNum);
  16.   Filter.FilterGraph := FilterGraph;
  17.   FilterGraph.Active := true;
  18.   IBF := Filter as IBaseFilter;  // Here error: "Invalid typecast"
  19.   PinList := TPinList.Create(IBF);
  20.   VideoMediaTypes.Assign(PinList.First);
  21.   CBVideoFormats.Clear;
  22.   for Cnt := 0 to Pred(VideoMediaTypes.Count) do
  23.   begin
  24.    MediaStr := VideoMediaTypes.MediaDescription[Cnt];
  25.    CBVideoFormats.Items.Add(MediaStr);
  26.   end;
  27.   if(CBVideoFormats.Items.Count <> 0) then CBVideoFormats.ItemIndex := 0;
  28.  finally
  29.   FreeAndNil(PinList);
  30.  end;
  31. end;              
  32.  
  33. B->  
  34.  
Regards,
Dzandaa

Dzandaa

  • Sr. Member
  • ****
  • Posts: 391
  • From C# to Lazarus
Hi again,

I also tried on Windows 10 Home 64 Bits with Lazarus 3.4 FPC 3.2.2: same result...
No problem in Windows 7 Pro 64 Bits

Did I miss something?

B->

P.S. DxDiag show DirectX 12
Regards,
Dzandaa

af0815

  • Hero Member
  • *****
  • Posts: 1379
Do you use the DSPack from my Source ?

Here "Lazarus 3.4 (rev lazarus_3_4) FPC 3.2.3 i386-win32-win32/win64" it compiles (and with all Lazarus/fpc Versions on my systems) - but i use only win32 Versions of Lazarus because of some issues with 64Bit Lazarus in older (ancient) Programms.
regards
Andreas

Dzandaa

  • Sr. Member
  • ****
  • Posts: 391
  • From C# to Lazarus
Hi,

Same problem with cross compiling Win64->Win32

I stop playing with video on Windows. My program works well on Linux, I was hoping it was Linux/Windows portable.
It works on Windows 7 64 Bits.

B->
Regards,
Dzandaa

VisualLab

  • Hero Member
  • *****
  • Posts: 573
Hi again,

I also tried on Windows 10 Home 64 Bits with Lazarus 3.4 FPC 3.2.2: same result...
No problem in Windows 7 Pro 64 Bits

Did I miss something?

B->

P.S. DxDiag show DirectX 12

DirectShow has not been part of DirectX since at least Windows 7 (if not earlier). I suggest following the advice of af0815. I also used this package, although in Delphi. Anyway, it works quite well.

 

TinyPortal © 2005-2018