static void Main(string[] args) { Console.WriteLine("上传图片"); var nvc = new NameValueCollection { { "deviceId", "2" } }; var result = ImageClient.Upload(@"C:\Users\Administrator\Desktop\公共数据库系统建设\新建文件夹\slide_02.png", new NameValueCollection()); Console.WriteLine(result); Console.WriteLine("写入数据库"); var entity = new Images { ImageUrl = result.ImageUrl, }; var providre = new ImageDataRepository(); providre.Add(entity); Console.ReadLine(); }
/// <summary> /// Photo bayan detector method which uses OpenCV. NB: it saves image data to the DB, which is passed to it, if the image is not bayan. /// </summary> /// <param name="image">Image to check.</param> /// <param name="messageData">Message data.</param> /// <returns>BayanResult object.</returns> public static BayanResult DetectPhotoBayan(Image image, MessageData messageData) { var detector = new ImageFeatureDeteсtor(); var repository = new ImageDataRepository(); var similarityLimit = Settings.Get <float>("similarity"); var descriptors = detector.GetDescriptors(image); var imagesData = repository.GetForChat(messageData.ChatId); var tasks = new List <Task <BayanResult> >(imagesData.Count); var cancellationTokenSource = new CancellationTokenSource(); tasks.AddRange(imagesData.Select(imageData => Task.Factory.StartNew(() => { var result = new BayanResult(); var similarity = detector.GetSimilarity(descriptors, imageData.GetDescriptors()); // Bayan detected if (similarity >= similarityLimit) { result.IsBayan = true; result.Bayanity = similarity; result.OriginalImage = imageData; } return(result); }, cancellationTokenSource.Token))); var totalResult = new BayanResult(); while (tasks.Count > 0) { var i = Task.WaitAny(tasks.ToArray()); var taskResult = tasks[i].Result; if (taskResult.IsBayan) { // Cancell all tasks cancellationTokenSource.Cancel(); totalResult = taskResult; return(totalResult); } tasks.RemoveAt(i); } // If is not bayan, save the image data var newImageData = new ImageData(descriptors) { ChatId = messageData.ChatId, UserId = messageData.UserId, DateTimeAdded = messageData.DateTimeAdded, MessageId = messageData.MessageId, UserFullName = messageData.UserFullName, UserName = messageData.UserName }; repository.Insert(newImageData); return(totalResult); }
private ImageDataService() { repo = new ImageDataRepository(); }