如何使用 C# 显示 json 数组中的特定数据
How to show specific data from json array using C#
我目前正在做车辆的车牌识别。车牌检测使用来自 openAlpr 的 API。我不知道如何从 json 字符串中显示准确的车牌,因为第一个结果显示最准确的车牌。另外我正在使用 c# 进行编码
private static readonly HttpClient client = new HttpClient();
List<Image> images;
List<VehicleResult> vehicleResult;
public static async Task<string> ProcessImage(string image_path)
{
string SECRET_KEY = "";
Byte[] bytes = File.ReadAllBytes(image_path);
string imagebase64 = Convert.ToBase64String(bytes);
var content = new StringContent(imagebase64);
var response = await client.PostAsync("https://api.openalpr.com/v2/recognize_bytes?recognize_vehicle=1&country=my&secret_key=" + SECRET_KEY, content).ConfigureAwait(false);
var buffer = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
var byteArray = buffer.ToArray();
var responseString = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);
return responseString;
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Multiselect = false;
openFileDialog1.Filter = "Image Files |*.JPG; *.JPEG; *.PNG; *.BMP|JPEG Image |*.JPG; *.JPEG|PNG Image |*.PNG|Bitmap |*.BMP";
openFileDialog1.Title = "Select an Image";
openFileDialog1.FileName = "";
if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtPath.Text = openFileDialog1.FileName;
pbImage.Image = Image.FromFile(txtPath.Text);
}
}
private void button2_Click(object sender, EventArgs e)
{
Task<string> recognizeTask = Task.Run(() => ProcessImage(txtPath.Text));
recognizeTask.Wait();
string task_result = recognizeTask.Result;
richTextBox1.Text = task_result;
}
VehicleResult.cs
public class VehicleResult
{
public class ProcessingTime
{
public double total { get; set; }
public double plates { get; set; }
public double vehicles { get; set; }
}
public class VehicleRegion
{
public int y { get; set; }
public int x { get; set; }
public int height { get; set; }
public int width { get; set; }
}
public class Candidate
{
public int matches_template { get; set; }
public string plate { get; set; }
public double confidence { get; set; }
}
public class Coordinate
{
public int y { get; set; }
public int x { get; set; }
}
public class Orientation
{
public double confidence { get; set; }
public string name { get; set; }
}
public class Color
{
public double confidence { get; set; }
public string name { get; set; }
}
public class Make
{
public double confidence { get; set; }
public string name { get; set; }
}
public class BodyType
{
public double confidence { get; set; }
public string name { get; set; }
}
public class Year
{
public double confidence { get; set; }
public string name { get; set; }
}
public class MakeModel
{
public double confidence { get; set; }
public string name { get; set; }
}
public class Vehicle
{
public List<Orientation> orientation { get; set; }
public List<Color> color { get; set; }
public List<Make> make { get; set; }
public List<BodyType> body_type { get; set; }
public List<Year> year { get; set; }
public List<MakeModel> make_model { get; set; }
}
public class Result
{
public string plate { get; set; }
public double confidence { get; set; }
public int region_confidence { get; set; }
public VehicleRegion vehicle_region { get; set; }
public string region { get; set; }
public int plate_index { get; set; }
public double processing_time_ms { get; set; }
public List<Candidate> candidates { get; set; }
public List<Coordinate> coordinates { get; set; }
public Vehicle vehicle { get; set; }
public int matches_template { get; set; }
public int requested_topn { get; set; }
}
public class RegionsOfInterest
{
public int y { get; set; }
public int x { get; set; }
public int height { get; set; }
public int width { get; set; }
}
public class RootObject
{
public string uuid { get; set; }
public string data_type { get; set; }
public long epoch_time { get; set; }
public ProcessingTime processing_time { get; set; }
public int img_height { get; set; }
public int img_width { get; set; }
public List<Result> results { get; set; }
public int credits_monthly_used { get; set; }
public int version { get; set; }
public int credits_monthly_total { get; set; }
public bool error { get; set; }
public List<RegionsOfInterest> regions_of_interest { get; set; }
public int credit_cost { get; set; }
}
}
预期的输出是显示准确的结果。 result.plate[0]。但它显示
{"uuid": "", "data_type": "alpr_results", "epoch_time": 1557305608613, "processing_time": {"total": 602.8630000073463, "plates": 75.81536102294922, "vehicles": 481.9020000140881}, "img_height": 260, "img_width": 320, "results": [{"plate": "PEN15", "confidence": 92.73094177246094, "region_confidence": 78, "vehicle_region": {"y": 0, "x": 30, "height": 260, "width": 260}, "region": "sg-sg", "plate_index": 0, "processing_time_ms": 37.247642517089844, "candidates": [{"matches_template": 0, "plate": "PEN15", "confidence": 92.73094177246094}, {"matches_template": 0, "plate": "PN15", "confidence": 75.43009948730469}, {"matches_template": 0, "plate": "PFN15", "confidence": 74.60518646240234}, {"matches_template": 0, "plate": "PBN15", "confidence": 74.27806091308594}, {"matches_template": 0, "plate": "PGN15", "confidence": 74.27455139160156}, {"matches_template": 0, "plate": "PRN15", "confidence": 74.27429962158203}, {"matches_template": 0, "plate": "PHN15", "confidence": 74.2699203491211}, {"matches_template": 0, "plate": "PAN15", "confidence": 74.26849365234375}, {"matches_template": 0, "plate": "PPN15", "confidence": 74.26837921142578}], "coordinates": [{"y": 113, "x": 113}, {"y": 112, "x": 219}, {"y": 152, "x": 219}, {"y": 154, "x": 113}], "vehicle": {"orientation": [{"confidence": 99.92472076416016, "name": "180"}, {"confidence": 0.06104166433215141, "name": "225"}, {"confidence": 0.014204738661646843, "name": "135"}, {"confidence": 4.418209209688939e-05, "name": "0"}, {"confidence": 5.028776399740309e-07, "name": "45"}, {"confidence": 4.3639047930810193e-07, "name": "315"}, {"confidence": 1.272492795578728e-07, "name": "270"}, {"confidence": 4.266717112955121e-08, "name": "missing"}, {"confidence": 2.7205052446910294e-11, "name": "90"}], "color": [{"confidence": 97.71161651611328, "name": "black"}, {"confidence": 1.7446043491363525, "name": "blue"}, {"confidence": 0.5321600437164307, "name": "green"}, {"confidence": 0.004391432274132967, "name": "brown"}, {"confidence": 0.0033008360769599676, "name": "red"}, {"confidence": 0.002272360259667039, "name": "silver-gray"}, {"confidence": 0.0009545944631099701, "name": "purple"}, {"confidence": 0.0005065985606051981, "name": "white"}, {"confidence": 0.0001811941765481606, "name": "gold-beige"}, {"confidence": 1.677216641837731e-05, "name": "yellow"}], "make": [{"confidence": 99.99989318847656, "name": "honda"}, {"confidence": 9.245928231393918e-05, "name": "hyundai"}, {"confidence": 6.941896572243422e-06, "name": "saturn"}, {"confidence": 1.8492128219804727e-06, "name": "acura"}, {"confidence": 1.3430683338810923e-06, "name": "kia"}, {"confidence": 3.18417647804381e-07, "name": "lincoln"}, {"confidence": 6.216215808763081e-08, "name": "subaru"}, {"confidence": 4.7315740658859795e-08, "name": "mercury"}, {"confidence": 1.715258335366343e-08, "name": "audi"}, {"confidence": 1.2835081442119645e-08, "name": "mazda"}], "body_type": [{"confidence": 99.86608123779297, "name": "sedan-standard"}, {"confidence": 0.0997997596859932, "name": "van-mini"}, {"confidence": 0.025146393105387688, "name": "motorcycle"}, {"confidence": 0.008843712508678436, "name": "sedan-wagon"}, {"confidence": 4.827972952625714e-05, "name": "suv-crossover"}, {"confidence": 3.287095751147717e-05, "name": "sedan-sports"}, {"confidence": 2.3701748432358727e-05, "name": "suv-standard"}, {"confidence": 7.762670975353103e-06, "name": "truck-standard"}, {"confidence": 4.17470346292248e-06, "name": "sedan-compact"}, {"confidence": 8.812694431981072e-07, "name": "sedan-convertible"}], "year": [{"confidence": 88.54766845703125, "name": "2005-2009"}, {"confidence": 11.253129959106445, "name": "2000-2004"}, {"confidence": 0.16220106184482574, "name": "2010-2014"}, {"confidence": 0.03671129420399666, "name": "1995-1999"}, {"confidence": 0.00027290522120893, "name": "1990-1994"}, {"confidence": 9.778331332199741e-06, "name": "2015-2019"}, {"confidence": 1.1712587877354963e-07, "name": "1985-1989"}, {"confidence": 6.770891403107271e-09, "name": "1980-1984"}, {"confidence": 1.4306629214644317e-09, "name": "missing"}], "make_model": [{"confidence": 98.48702239990234, "name": "honda_accord"}, {"confidence": 0.8913977146148682, "name": "honda_odyssey"}, {"confidence": 0.5837093591690063, "name": "honda_civic"}, {"confidence": 0.01721840724349022, "name": "honda_accord-euro"}, {"confidence": 0.009933937340974808, "name": "hyundai_sonata"}, {"confidence": 0.003931120038032532, "name": "honda_stream"}, {"confidence": 0.0024787778966128826, "name": "honda_elysion"}, {"confidence": 0.0017656905110925436, "name": "audi_a6"}, {"confidence": 0.0008376066107302904, "name": "hyundai_azera"}, {"confidence": 0.0006411963258869946, "name": "honda_inspire"}]}, "matches_template": 0, "requested_topn": 10}], "credits_monthly_used": 78, "version": 2, "credits_monthly_total": 2000, "error": false, "regions_of_interest": [{"y": 0, "x": 0, "height": 260, "width": 320}], "credit_cost": 2}
The lpr system
您需要解析 JSON 响应以获取车牌号。
result[0].plate
小心,不要在 Whosebug
上发布您的 secret key
我目前正在做车辆的车牌识别。车牌检测使用来自 openAlpr 的 API。我不知道如何从 json 字符串中显示准确的车牌,因为第一个结果显示最准确的车牌。另外我正在使用 c# 进行编码
private static readonly HttpClient client = new HttpClient();
List<Image> images;
List<VehicleResult> vehicleResult;
public static async Task<string> ProcessImage(string image_path)
{
string SECRET_KEY = "";
Byte[] bytes = File.ReadAllBytes(image_path);
string imagebase64 = Convert.ToBase64String(bytes);
var content = new StringContent(imagebase64);
var response = await client.PostAsync("https://api.openalpr.com/v2/recognize_bytes?recognize_vehicle=1&country=my&secret_key=" + SECRET_KEY, content).ConfigureAwait(false);
var buffer = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
var byteArray = buffer.ToArray();
var responseString = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);
return responseString;
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Multiselect = false;
openFileDialog1.Filter = "Image Files |*.JPG; *.JPEG; *.PNG; *.BMP|JPEG Image |*.JPG; *.JPEG|PNG Image |*.PNG|Bitmap |*.BMP";
openFileDialog1.Title = "Select an Image";
openFileDialog1.FileName = "";
if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtPath.Text = openFileDialog1.FileName;
pbImage.Image = Image.FromFile(txtPath.Text);
}
}
private void button2_Click(object sender, EventArgs e)
{
Task<string> recognizeTask = Task.Run(() => ProcessImage(txtPath.Text));
recognizeTask.Wait();
string task_result = recognizeTask.Result;
richTextBox1.Text = task_result;
}
VehicleResult.cs
public class VehicleResult
{
public class ProcessingTime
{
public double total { get; set; }
public double plates { get; set; }
public double vehicles { get; set; }
}
public class VehicleRegion
{
public int y { get; set; }
public int x { get; set; }
public int height { get; set; }
public int width { get; set; }
}
public class Candidate
{
public int matches_template { get; set; }
public string plate { get; set; }
public double confidence { get; set; }
}
public class Coordinate
{
public int y { get; set; }
public int x { get; set; }
}
public class Orientation
{
public double confidence { get; set; }
public string name { get; set; }
}
public class Color
{
public double confidence { get; set; }
public string name { get; set; }
}
public class Make
{
public double confidence { get; set; }
public string name { get; set; }
}
public class BodyType
{
public double confidence { get; set; }
public string name { get; set; }
}
public class Year
{
public double confidence { get; set; }
public string name { get; set; }
}
public class MakeModel
{
public double confidence { get; set; }
public string name { get; set; }
}
public class Vehicle
{
public List<Orientation> orientation { get; set; }
public List<Color> color { get; set; }
public List<Make> make { get; set; }
public List<BodyType> body_type { get; set; }
public List<Year> year { get; set; }
public List<MakeModel> make_model { get; set; }
}
public class Result
{
public string plate { get; set; }
public double confidence { get; set; }
public int region_confidence { get; set; }
public VehicleRegion vehicle_region { get; set; }
public string region { get; set; }
public int plate_index { get; set; }
public double processing_time_ms { get; set; }
public List<Candidate> candidates { get; set; }
public List<Coordinate> coordinates { get; set; }
public Vehicle vehicle { get; set; }
public int matches_template { get; set; }
public int requested_topn { get; set; }
}
public class RegionsOfInterest
{
public int y { get; set; }
public int x { get; set; }
public int height { get; set; }
public int width { get; set; }
}
public class RootObject
{
public string uuid { get; set; }
public string data_type { get; set; }
public long epoch_time { get; set; }
public ProcessingTime processing_time { get; set; }
public int img_height { get; set; }
public int img_width { get; set; }
public List<Result> results { get; set; }
public int credits_monthly_used { get; set; }
public int version { get; set; }
public int credits_monthly_total { get; set; }
public bool error { get; set; }
public List<RegionsOfInterest> regions_of_interest { get; set; }
public int credit_cost { get; set; }
}
}
预期的输出是显示准确的结果。 result.plate[0]。但它显示
{"uuid": "", "data_type": "alpr_results", "epoch_time": 1557305608613, "processing_time": {"total": 602.8630000073463, "plates": 75.81536102294922, "vehicles": 481.9020000140881}, "img_height": 260, "img_width": 320, "results": [{"plate": "PEN15", "confidence": 92.73094177246094, "region_confidence": 78, "vehicle_region": {"y": 0, "x": 30, "height": 260, "width": 260}, "region": "sg-sg", "plate_index": 0, "processing_time_ms": 37.247642517089844, "candidates": [{"matches_template": 0, "plate": "PEN15", "confidence": 92.73094177246094}, {"matches_template": 0, "plate": "PN15", "confidence": 75.43009948730469}, {"matches_template": 0, "plate": "PFN15", "confidence": 74.60518646240234}, {"matches_template": 0, "plate": "PBN15", "confidence": 74.27806091308594}, {"matches_template": 0, "plate": "PGN15", "confidence": 74.27455139160156}, {"matches_template": 0, "plate": "PRN15", "confidence": 74.27429962158203}, {"matches_template": 0, "plate": "PHN15", "confidence": 74.2699203491211}, {"matches_template": 0, "plate": "PAN15", "confidence": 74.26849365234375}, {"matches_template": 0, "plate": "PPN15", "confidence": 74.26837921142578}], "coordinates": [{"y": 113, "x": 113}, {"y": 112, "x": 219}, {"y": 152, "x": 219}, {"y": 154, "x": 113}], "vehicle": {"orientation": [{"confidence": 99.92472076416016, "name": "180"}, {"confidence": 0.06104166433215141, "name": "225"}, {"confidence": 0.014204738661646843, "name": "135"}, {"confidence": 4.418209209688939e-05, "name": "0"}, {"confidence": 5.028776399740309e-07, "name": "45"}, {"confidence": 4.3639047930810193e-07, "name": "315"}, {"confidence": 1.272492795578728e-07, "name": "270"}, {"confidence": 4.266717112955121e-08, "name": "missing"}, {"confidence": 2.7205052446910294e-11, "name": "90"}], "color": [{"confidence": 97.71161651611328, "name": "black"}, {"confidence": 1.7446043491363525, "name": "blue"}, {"confidence": 0.5321600437164307, "name": "green"}, {"confidence": 0.004391432274132967, "name": "brown"}, {"confidence": 0.0033008360769599676, "name": "red"}, {"confidence": 0.002272360259667039, "name": "silver-gray"}, {"confidence": 0.0009545944631099701, "name": "purple"}, {"confidence": 0.0005065985606051981, "name": "white"}, {"confidence": 0.0001811941765481606, "name": "gold-beige"}, {"confidence": 1.677216641837731e-05, "name": "yellow"}], "make": [{"confidence": 99.99989318847656, "name": "honda"}, {"confidence": 9.245928231393918e-05, "name": "hyundai"}, {"confidence": 6.941896572243422e-06, "name": "saturn"}, {"confidence": 1.8492128219804727e-06, "name": "acura"}, {"confidence": 1.3430683338810923e-06, "name": "kia"}, {"confidence": 3.18417647804381e-07, "name": "lincoln"}, {"confidence": 6.216215808763081e-08, "name": "subaru"}, {"confidence": 4.7315740658859795e-08, "name": "mercury"}, {"confidence": 1.715258335366343e-08, "name": "audi"}, {"confidence": 1.2835081442119645e-08, "name": "mazda"}], "body_type": [{"confidence": 99.86608123779297, "name": "sedan-standard"}, {"confidence": 0.0997997596859932, "name": "van-mini"}, {"confidence": 0.025146393105387688, "name": "motorcycle"}, {"confidence": 0.008843712508678436, "name": "sedan-wagon"}, {"confidence": 4.827972952625714e-05, "name": "suv-crossover"}, {"confidence": 3.287095751147717e-05, "name": "sedan-sports"}, {"confidence": 2.3701748432358727e-05, "name": "suv-standard"}, {"confidence": 7.762670975353103e-06, "name": "truck-standard"}, {"confidence": 4.17470346292248e-06, "name": "sedan-compact"}, {"confidence": 8.812694431981072e-07, "name": "sedan-convertible"}], "year": [{"confidence": 88.54766845703125, "name": "2005-2009"}, {"confidence": 11.253129959106445, "name": "2000-2004"}, {"confidence": 0.16220106184482574, "name": "2010-2014"}, {"confidence": 0.03671129420399666, "name": "1995-1999"}, {"confidence": 0.00027290522120893, "name": "1990-1994"}, {"confidence": 9.778331332199741e-06, "name": "2015-2019"}, {"confidence": 1.1712587877354963e-07, "name": "1985-1989"}, {"confidence": 6.770891403107271e-09, "name": "1980-1984"}, {"confidence": 1.4306629214644317e-09, "name": "missing"}], "make_model": [{"confidence": 98.48702239990234, "name": "honda_accord"}, {"confidence": 0.8913977146148682, "name": "honda_odyssey"}, {"confidence": 0.5837093591690063, "name": "honda_civic"}, {"confidence": 0.01721840724349022, "name": "honda_accord-euro"}, {"confidence": 0.009933937340974808, "name": "hyundai_sonata"}, {"confidence": 0.003931120038032532, "name": "honda_stream"}, {"confidence": 0.0024787778966128826, "name": "honda_elysion"}, {"confidence": 0.0017656905110925436, "name": "audi_a6"}, {"confidence": 0.0008376066107302904, "name": "hyundai_azera"}, {"confidence": 0.0006411963258869946, "name": "honda_inspire"}]}, "matches_template": 0, "requested_topn": 10}], "credits_monthly_used": 78, "version": 2, "credits_monthly_total": 2000, "error": false, "regions_of_interest": [{"y": 0, "x": 0, "height": 260, "width": 320}], "credit_cost": 2}
The lpr system
您需要解析 JSON 响应以获取车牌号。
result[0].plate
小心,不要在 Whosebug
上发布您的secret key