Integrating IronPython with a Windows Form Application

  • Thread starter Thread starter GFK_010
  • Start date Start date
G

GFK_010

Guest
As per the title, I am having a hard time getting this code to work. Here is the idea. Browse for a .MP4 file, break it into its component frames, and for each frame, use template detection to look for a paw. I am using a Windows Form Application for C# language.

The error message is at the very bottom of the C# code, marked with ***. The errorm message is

Microsoft.Scripting.SyntaxErrorException: 'unexpected token ':''

I cannot find a single thread that relates to this error token. I have ran the test every which way, removing and moving colons (hehe) every which way to see if it will work, and the tokens are all the same.


I have included the following NuGet Packages:

Accord.Video.FFMPEG

IronPython 2.7.9

IronPython.StdLib

Dynamic Language Runtime



Here is the C# code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using IronPython.Hosting;
using Accord.Video.FFMPEG;

namespace FileScanner
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
Button1.Enabled = true;
Button2.Enabled = false;
}

public void Button1_Click(object sender, EventArgs e)
{
//references: OpenFileDialog In C#

OpenFileDialog finder = new OpenFileDialog()
{
InitialDirectory = @"C:\",
Title = "Find .MP4",

RestoreDirectory = true,

CheckFileExists = true,
CheckPathExists = true,

Filter = "All files (*.*)|*.*"

};

if(finder.ShowDialog() == DialogResult.OK)
{
RichTextBox1.Text = finder.FileName;
Button2.Enabled = true;
}
}

private void RichTextBox1_TextChanged(object sender, EventArgs e)
{

}

private void Button2_Click(object sender, EventArgs e)
{
//references: try-catch - C# Reference

CheckFile();
BreakFile();
}

private void CheckFile()
{
try
{
string path = RichTextBox1.Text;
}

catch (ArgumentException e) when (e.ParamName == "")
{
MessageBox.Show("File Not Found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void BreakFile()
{
VideoFileReader reader = new VideoFileReader();
reader.Open(RichTextBox1.Text);

var framesToCheck = reader.FrameCount * reader.FrameRate;

for (int d = 0; d < framesToCheck; d++)
{
Bitmap videoFrame = reader.ReadVideoFrame(d);

ScanFile(videoFrame);

videoFrame.Dispose();
}
}

private void ScanFile(Bitmap img)
{
//References:
View: https://www.youtube.com/watch?v=g1VWGdHRkHs

// 1. Getting Started - IronPython Documentation

var engine = Python.CreateEngine();
var script = @"C:\Users\BeckerLab\source\repos\FileScanner\PythonScanner\PythonScanner.py";
var source = engine.CreateScriptSourceFromString(script);

// C:\Users\BeckerLab\source\repos\ContingencyTestTwo\PythAnalysis\PythAnalysis.py

var fling = img;

//#references: Object Detection with Less Than 10 Lines of Code Using Python
//# arunponnusamy/cvlib
//# Python Programming Tutorials

var eIO = engine.Runtime.IO;
var errors = new System.IO.MemoryStream();
eIO.SetErrorOutput(errors, Encoding.Default);

var results = new System.IO.MemoryStream();
eIO.SetOutput(results, Encoding.Default);

var scope = engine.CreateScope();
engine.GetSysModule().SetVariable("image", fling);
source.Execute(scope); *** Here is where error hits
}
}
}



Here is the Python File


import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
import cvlib.object_detection

image = sys.fling

I = cv2.imread(image)
Ig = cv2.cvtColor(I, cv2.COLOR_BGR2GRAY)

temp_imgR = cv2.imread('C:\\Users\BeckerLab\source\repos\FileScanner\Right.jpg', 0) #Step 3. Collect metrics from the analysis
f,g = temp_imgR.shape[::-1]

temp_imgL = cv2.imread('C:\\Users\BeckerLab\source\repos\FileScanner\Left.jpg', 0)
a,b = temp_imgL.shape[::-1]

thresh = 0.8

resR = cv2.matchTemplate(Ig, temp_imgR, cv2.TM_CCOEFF_NORMED)
resL = cv2.matchTemplate(Ig, temp_imgL, cv2.TM_CCOEFF_NORMED)

locR = np.where(resR >= thresh)
locL = np.where(resL >= thresh)

#for pt in zip(*locR[::-1]):
# cv2.rectangle(I, pt, (pt[0]+f, pt[1]+g), (0,0,255), 1)
# numR += 1
# if numR == 1:
# break

#for qt in zip(*locL[::-1]):
# cv2.rectangle(I, qt, (qt[0]+a, qt[1]+b), (255,0,0), 1)
# numL += 1
# if numL == 1:
# break

#plt.imshow(output_image) #Step 5. Return the results of the image.
#plt.show()

sys.fling = I

plt.imshow(I)
plt.show
cv2.destroyAllWindows()

*Can't show images yet, apparently. Too new to this site.

Continue reading...
 
Back
Top