12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using AIPractice.LabellerServer.Managers;
- using AIPractice.LabellerServer.Managers.Entities;
- using AIPractice.LabellerServer.ViewModels;
- using AIPractice.Shared.ImageRois;
- using MailKit.Net.Smtp;
- using MailKit.Security;
- using MimeKit;
- using PdfSharp;
- using PdfSharp.Drawing;
- using PdfSharp.Pdf;
- namespace AIPractice.LabellerServer.WPF
- {
- public class ReportDocument:IDisposable
- {
- private int _pageCount;
- private readonly PdfDocument _document;
- public ReportDocument(PdfDocument document)
- {
- _document = document;
- }
- public ReportPage CreateCoverPage()
- {
- _pageCount++;
- return new CoverPage(_document.AddPage(), _pageCount);
- }
- public ReportPage CreateContentPage()
- {
- _pageCount++;
- return new ContentPage(_document.AddPage(), _pageCount);
- }
- public void Save(string filePath)
- {
- _document.Options.FlateEncodeMode = PdfFlateEncodeMode.BestCompression;
- _document.Options.UseFlateDecoderForJpegImages = PdfUseFlateDecoderForJpegImages.Always;
- _document.Options.EnableCcittCompressionForBilevelImages = true;
- _document.Options.NoCompression = false;
- _document.Options.CompressContentStreams = true;
- _document.Save(filePath);
- }
- public void Save(Stream stream)
- {
- _document.Options.FlateEncodeMode = PdfFlateEncodeMode.BestCompression;
- _document.Options.UseFlateDecoderForJpegImages = PdfUseFlateDecoderForJpegImages.Always;
- _document.Options.EnableCcittCompressionForBilevelImages = true;
- _document.Options.NoCompression = false;
- _document.Options.CompressContentStreams = true;
- _document.Save(stream);
- }
- public void Dispose()
- {
- _document?.Dispose();
- }
- }
- public class ReportPage
- {
- protected readonly PdfPage Page;
- private int _index;
- public ReportPage(PdfPage page, int index)
- {
- _index = index;
- Page = page;
- Page.Size = PageSize.A3;
- }
- public virtual void Render()
- {
- using (XGraphics g = XGraphics.FromPdfPage(Page))
- {
- var indexVisual = new DrawingVisual();
- var indexContext = indexVisual.RenderOpen();
- var indexText = new FormattedText(
- $"Page {_index}",
- CultureInfo.GetCultureInfo("zh-cn"),
- FlowDirection.LeftToRight,
- new Typeface(new FontFamily("Microsoft YaHei"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
- 16,
- Brushes.Black);
- indexContext.DrawText(indexText, new Point(0, 0));
- indexContext.Close();
- BitmapSource indexImage = new RenderTargetBitmap((int)indexText.Width, (int)indexText.Height, 96, 96,
- PixelFormats.Default);
- ((RenderTargetBitmap)indexImage).Render(indexVisual);
- g.DrawImage(XImage.FromBitmapSource(indexImage), new XPoint(4, 4));
- }
- }
- }
- public class CoverPage : ReportPage
- {
- public string Title { get; set; }
- public string UserName { get; set; }
- public CoverPage(PdfPage page, int index) : base(page, index)
- {
- }
- public override void Render()
- {
- base.Render();
- using (XGraphics g = XGraphics.FromPdfPage(Page))
- {
- var titleVisual = new DrawingVisual();
- var titleContext = titleVisual.RenderOpen();
- var titleText = new FormattedText(
- Title,
- CultureInfo.GetCultureInfo("zh-cn"),
- FlowDirection.LeftToRight,
- new Typeface(new FontFamily("Microsoft YaHei"), FontStyles.Normal, FontWeights.Bold, FontStretches.Normal),
- 32,
- Brushes.Black);
- titleContext.DrawText(titleText, new Point(0, 0));
- titleContext.Close();
- BitmapSource titleImage = new RenderTargetBitmap((int)titleText.Width, (int)titleText.Height, 96, 96, PixelFormats.Default);
- ((RenderTargetBitmap)titleImage).Render(titleVisual);
- if (titleImage.Width > Page.Width)
- {
- var scale = Page.Width / titleImage.Width;
- titleImage = new TransformedBitmap(titleImage, new ScaleTransform(scale, scale));
- }
- var image = XImage.FromBitmapSource(titleImage);
- var x = (Page.Width - image.PixelWidth * 72 / image.HorizontalResolution) / 2;
- g.DrawImage(image, x, Page.Height / 2 - titleImage.Height - 4);
- var userVisual = new DrawingVisual();
- var userContext = userVisual.RenderOpen();
- var userText = new FormattedText(
- UserName,
- CultureInfo.GetCultureInfo("zh-cn"),
- FlowDirection.LeftToRight,
- new Typeface(new FontFamily("Microsoft YaHei"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
- 20,
- Brushes.Black);
- userContext.DrawText(userText, new Point(0, 0));
- userContext.Close();
- BitmapSource userImage = new RenderTargetBitmap((int)userText.Width, (int)userText.Height, 96, 96, PixelFormats.Default);
- ((RenderTargetBitmap)userImage).Render(userVisual);
- if (userImage.Width > Page.Width)
- {
- var scale = Page.Width / userImage.Width;
- userImage = new TransformedBitmap(userImage, new ScaleTransform(scale, scale));
- }
- image = XImage.FromBitmapSource(userImage);
- x = (Page.Width - image.PixelWidth * 72 / image.HorizontalResolution) / 2;
- g.DrawImage(image, x, Page.Height / 2 + userImage.Height + 4);
- }
- }
- }
- public class ContentPage : ReportPage
- {
- public string Conclusion { get; set; }
- public string BestConclusion { get; set; }
- public string ReviewDescription { get; set; }
- public BitmapSource OriginalImage { get; set; }
- public BitmapSource LabeledImage { get; set; }
- public BitmapSource BestImage { get; set; }
- public ContentPage(PdfPage page, int index) : base(page, index)
- {
- }
- public override void Render()
- {
- base.Render();
- var contentHeight = 0d;
- using (XGraphics g = XGraphics.FromPdfPage(Page))
- {
- var imageTitleVisual1 = new DrawingVisual();
- var imageTitleContext1 = imageTitleVisual1.RenderOpen();
- var imageTitleText1 = new FormattedText(
- "原始图",
- CultureInfo.GetCultureInfo("zh-cn"),
- FlowDirection.LeftToRight,
- new Typeface(new FontFamily("Microsoft YaHei"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
- 16,
- Brushes.Black);
- imageTitleContext1.DrawText(imageTitleText1, new Point(0, 0));
- imageTitleContext1.Close();
- BitmapSource titleBitmap1 = new RenderTargetBitmap((int)imageTitleText1.Width, (int)imageTitleText1.Height, 96, 96, PixelFormats.Default);
- ((RenderTargetBitmap)titleBitmap1).Render(imageTitleVisual1);
- var titleImage1 = XImage.FromBitmapSource(titleBitmap1);
- var imageTitleVisual2 = new DrawingVisual();
- var imageTitleContext2 = imageTitleVisual2.RenderOpen();
- var imageTitleText2 = new FormattedText(
- "标注图",
- CultureInfo.GetCultureInfo("zh-cn"),
- FlowDirection.LeftToRight,
- new Typeface(new FontFamily("Microsoft YaHei"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
- 16,
- Brushes.Black);
- imageTitleContext2.DrawText(imageTitleText2, new Point(0, 0));
- imageTitleContext2.Close();
- BitmapSource titleBitmap2 = new RenderTargetBitmap((int)imageTitleText2.Width, (int)imageTitleText2.Height, 96, 96, PixelFormats.Default);
- ((RenderTargetBitmap)titleBitmap2).Render(imageTitleVisual2);
- var titleImage2 = XImage.FromBitmapSource(titleBitmap2);
- var imageTitleVisual3 = new DrawingVisual();
- var imageTitleContext3 = imageTitleVisual3.RenderOpen();
- var imageTitleText3 = new FormattedText(
- "最佳标注图",
- CultureInfo.GetCultureInfo("zh-cn"),
- FlowDirection.LeftToRight,
- new Typeface(new FontFamily("Microsoft YaHei"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
- 16,
- Brushes.Black);
- imageTitleContext3.DrawText(imageTitleText3, new Point(0, 0));
- imageTitleContext3.Close();
- BitmapSource titleBitmap3 = new RenderTargetBitmap((int)imageTitleText3.Width, (int)imageTitleText3.Height, 96, 96, PixelFormats.Default);
- ((RenderTargetBitmap)titleBitmap3).Render(imageTitleVisual3);
- var titleImage3 = XImage.FromBitmapSource(titleBitmap3);
- var totalTitleHeight = titleImage1.PixelHeight * 72 /
- titleImage1.VerticalResolution +
- titleImage2.PixelHeight * 72 /
- titleImage2.VerticalResolution +
- titleImage3.PixelHeight * 72 /
- titleImage3.VerticalResolution;
- var imageHeight = (Page.Height / 4 * 3 -24 - totalTitleHeight -4)/3;
- BitmapSource originalBitmap = OriginalImage;
- var originalImage = XImage.FromBitmapSource(originalBitmap);
- var originalImageHeight = originalImage.PixelHeight * 72 / originalImage.VerticalResolution;
- if (originalImageHeight > imageHeight)
- {
- var scale = imageHeight / originalImageHeight;
- originalBitmap = new TransformedBitmap(originalBitmap, new ScaleTransform(scale, scale));
- originalImage = XImage.FromBitmapSource(originalBitmap);
- }
- var originalImageWidth = originalImage.PixelWidth * 72 / originalImage.HorizontalResolution;
- if (originalImageWidth > Page.Width)
- {
- var scale = Page.Width / originalImageWidth;
- originalBitmap = new TransformedBitmap(originalBitmap, new ScaleTransform(scale, scale));
- originalImage = XImage.FromBitmapSource(originalBitmap);
- }
- var x = (Page.Width - originalImage.PixelWidth * 72 / originalImage.HorizontalResolution) / 2;
- var y = 24d;
- g.DrawImage(originalImage, new XPoint(x, y));
- contentHeight += 24 + originalImage.PixelHeight * 72 / originalImage.VerticalResolution;
- x = (Page.Width - titleImage1.PixelWidth * 72 / titleImage1.HorizontalResolution) / 2;
- y = contentHeight + 2;
- g.DrawImage(titleImage1, new XPoint(x, y));
- contentHeight += 2 + titleImage1.PixelHeight * 72 / titleImage1.VerticalResolution;
- var labeledBitmap = LabeledImage;
- var labeledImage = XImage.FromBitmapSource(labeledBitmap);
- var labeledImageHeight = labeledImage.PixelHeight * 72 / labeledImage.VerticalResolution;
- if (labeledImageHeight > imageHeight)
- {
- var scale = imageHeight / labeledImageHeight;
- labeledBitmap = new TransformedBitmap(labeledBitmap, new ScaleTransform(scale, scale));
- labeledImage = XImage.FromBitmapSource(labeledBitmap);
- }
- var labeledImageWidth = labeledImage.PixelWidth * 72 / labeledImage.HorizontalResolution;
- if (labeledImageWidth > Page.Width)
- {
- var scale = Page.Width / labeledImageWidth;
- labeledBitmap = new TransformedBitmap(labeledBitmap, new ScaleTransform(scale, scale));
- labeledImage = XImage.FromBitmapSource(labeledBitmap);
- }
- x = (Page.Width - labeledImage.PixelWidth * 72 / labeledImage.HorizontalResolution) / 2;
- y = contentHeight + 2;
- g.DrawImage(labeledImage, new XPoint(x, y));
- contentHeight += 2 + labeledImage.PixelHeight * 72 / labeledImage.VerticalResolution;
- x = (Page.Width - titleImage2.PixelWidth * 72 / titleImage2.HorizontalResolution) / 2;
- y = contentHeight + 2;
- g.DrawImage(titleImage2, new XPoint(x, y));
- contentHeight += 2 + titleImage2.PixelHeight * 72 / titleImage2.VerticalResolution;
- var bestBitmap = BestImage;
- if (bestBitmap != null)
- {
- var bestImage = XImage.FromBitmapSource(bestBitmap);
- var bestImageHeight = bestImage.PixelHeight * 72 / bestImage.VerticalResolution;
- if (bestImageHeight > imageHeight)
- {
- var scale = imageHeight / bestImageHeight;
- bestBitmap = new TransformedBitmap(bestBitmap, new ScaleTransform(scale, scale));
- bestImage = XImage.FromBitmapSource(bestBitmap);
- }
- var bestImageWidth = bestImage.PixelWidth * 72 / bestImage.HorizontalResolution;
- if (bestImageWidth > Page.Width)
- {
- var scale = Page.Width / bestImageWidth;
- bestBitmap = new TransformedBitmap(bestBitmap, new ScaleTransform(scale, scale));
- bestImage = XImage.FromBitmapSource(bestBitmap);
- }
- x = (Page.Width - bestImage.PixelWidth * 72 / bestImage.HorizontalResolution) / 2;
- y = contentHeight + 2;
- g.DrawImage(bestImage, new XPoint(x, y));
- contentHeight += 2 + bestImage.PixelHeight * 72 / bestImage.VerticalResolution;
- x = (Page.Width - titleImage3.PixelWidth * 72 / titleImage3.HorizontalResolution) / 2;
- y = contentHeight + 2;
- g.DrawImage(titleImage3, new XPoint(x, y));
- contentHeight += 2 + titleImage3.PixelHeight * 72 / titleImage3.VerticalResolution;
- }
- var maxTextWidth = Page.Width * 96 / 72 - 8.0 * 96 / 72;
- var conclusionVisual = new DrawingVisual();
- var conclusionContext = conclusionVisual.RenderOpen();
- var conclusionTitle = new FormattedText(
- "标注结果:",
- CultureInfo.GetCultureInfo("zh-cn"),
- FlowDirection.LeftToRight,
- new Typeface(new FontFamily("Microsoft YaHei"), FontStyles.Normal, FontWeights.Bold, FontStretches.Normal),
- 22,
- Brushes.Black);
- var conclusionText = new FormattedText(
- Conclusion,
- CultureInfo.GetCultureInfo("zh-cn"),
- FlowDirection.LeftToRight,
- new Typeface(new FontFamily("Microsoft YaHei"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
- 14,
- Brushes.Black);
- conclusionText.MaxTextWidth = maxTextWidth;
- conclusionContext.DrawText(conclusionTitle, new Point(0, 0));
- conclusionContext.DrawText(conclusionText, new Point(0, conclusionTitle.Height + 4));
- conclusionContext.Close();
- var width = Math.Max(conclusionTitle.Width, conclusionText.Width);
- var height = conclusionTitle.Height + 4 + conclusionText.Height;
- BitmapSource conclusionBitmap = new RenderTargetBitmap((int)width, (int)height, 96, 96, PixelFormats.Default);
- ((RenderTargetBitmap)conclusionBitmap).Render(conclusionVisual);
- var conclusionImage = XImage.FromBitmapSource(conclusionBitmap);
- var imageWidth = conclusionImage.PixelWidth * 72 / conclusionImage.HorizontalResolution;
- if (imageWidth > (Page.Width - 8))
- {
- var scale = (Page.Width - 8) / imageWidth;
- conclusionBitmap = new TransformedBitmap(conclusionBitmap, new ScaleTransform(scale, scale));
- }
- conclusionImage = XImage.FromBitmapSource(conclusionBitmap);
- x = 8;
- y = contentHeight + 8;
- g.DrawImage(conclusionImage, new XPoint(x, y));
- contentHeight += 8 + conclusionImage.PixelHeight * 72 / conclusionImage.VerticalResolution;
- if (!string.IsNullOrEmpty(BestConclusion))
- {
- var bestConclusionVisual = new DrawingVisual();
- var bestConclusionContext = bestConclusionVisual.RenderOpen();
- var bestConclusionTitle = new FormattedText(
- "参考结论:",
- CultureInfo.GetCultureInfo("zh-cn"),
- FlowDirection.LeftToRight,
- new Typeface(new FontFamily("Microsoft YaHei"), FontStyles.Normal, FontWeights.Bold,
- FontStretches.Normal),
- 22,
- Brushes.Black);
- var bestConclusionText = new FormattedText(
- BestConclusion,
- CultureInfo.GetCultureInfo("zh-cn"),
- FlowDirection.LeftToRight,
- new Typeface(new FontFamily("Microsoft YaHei"), FontStyles.Normal, FontWeights.Normal,
- FontStretches.Normal),
- 14,
- Brushes.Blue);
- bestConclusionText.MaxTextWidth = maxTextWidth;
- bestConclusionContext.DrawText(bestConclusionTitle, new Point(0, 0));
- bestConclusionContext.DrawText(bestConclusionText, new Point(0, conclusionTitle.Height + 4));
- bestConclusionContext.Close();
- width = Math.Max(bestConclusionTitle.Width, bestConclusionText.Width);
- height = bestConclusionTitle.Height + 4 + bestConclusionText.Height;
- BitmapSource bestConclusionBitmap =
- new RenderTargetBitmap((int) width, (int) height, 96, 96, PixelFormats.Default);
- ((RenderTargetBitmap) bestConclusionBitmap).Render(bestConclusionVisual);
- var bestConclusionImage = XImage.FromBitmapSource(bestConclusionBitmap);
- imageWidth = bestConclusionImage.PixelWidth * 72 / bestConclusionImage.HorizontalResolution;
- if (imageWidth > (Page.Width - 8))
- {
- var scale = (Page.Width - 8) / imageWidth;
- bestConclusionBitmap =
- new TransformedBitmap(bestConclusionBitmap, new ScaleTransform(scale, scale));
- }
- bestConclusionImage = XImage.FromBitmapSource(bestConclusionBitmap);
- x = 8;
- y = contentHeight + 8;
- g.DrawImage(bestConclusionImage, new XPoint(x, y));
- contentHeight += 8 + bestConclusionImage.PixelHeight * 72 / bestConclusionImage.VerticalResolution;
- }
- var reviewVisual = new DrawingVisual();
- var reviewContext = reviewVisual.RenderOpen();
- var reviewTitle = new FormattedText(
- "审核结果:",
- CultureInfo.GetCultureInfo("zh-cn"),
- FlowDirection.LeftToRight,
- new Typeface(new FontFamily("Microsoft YaHei"), FontStyles.Normal, FontWeights.Bold, FontStretches.Normal),
- 22,
- Brushes.Black);
- var reviewText = new FormattedText(
- ReviewDescription,
- CultureInfo.GetCultureInfo("zh-cn"),
- FlowDirection.LeftToRight,
- new Typeface(new FontFamily("Microsoft YaHei"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
- 14,
- Brushes.Red);
- reviewText.MaxTextWidth = maxTextWidth;
- reviewContext.DrawText(reviewTitle, new Point(0, 0));
- reviewContext.DrawText(reviewText, new Point(0, reviewTitle.Height + 4));
- reviewContext.Close();
- width = Math.Max(reviewTitle.Width, reviewText.Width);
- height = reviewTitle.Height + 4 + reviewText.Height;
- BitmapSource reviewBitmap = new RenderTargetBitmap((int)width, (int)height, 96, 96, PixelFormats.Default);
- ((RenderTargetBitmap)reviewBitmap).Render(reviewVisual);
- var reviewImage = XImage.FromBitmapSource(reviewBitmap);
- imageWidth = reviewImage.PixelWidth * 72 / reviewImage.HorizontalResolution;
- if (imageWidth > (Page.Width - 8))
- {
- var scale = (Page.Width - 8) / imageWidth;
- reviewBitmap = new TransformedBitmap(reviewBitmap, new ScaleTransform(scale, scale));
- }
- reviewImage = XImage.FromBitmapSource(reviewBitmap);
- x = 8;
- y = contentHeight + 8;
- g.DrawImage(reviewImage, new XPoint(x, y));
- }
- }
- }
- public class ReviewInfo
- {
- public string Name { get; }
- public int Best { get; }
- public int Ok { get; }
- public int Bad { get; }
- public int Total => Best + Ok + Bad;
- public string BadLv
- {
- get
- {
- if (Bad == 0)
- {
- return "0%";
- }
- return $"{(int) ((double) Bad / Total * 100)}%";
- }
- }
- public ReviewInfo(string name, int best, int ok, int bad)
- {
- Best = best;
- Ok = ok;
- Bad = bad;
- Name = name;
- }
- }
- /// <summary>
- /// Interaction logic for ReportView.xaml
- /// </summary>
- public partial class ReportView : UserControl
- {
- private readonly ObservableCollection<AccountViewModel> _accountViewModels = new ObservableCollection<AccountViewModel>();
- public ReportView()
- {
- InitializeComponent();
- StartDate.SelectedDate = DateTime.Now.AddMonths(-1);
- EndDate.SelectedDate = DateTime.Now;
- AccountSelector.ItemsSource = _accountViewModels;
- LoadAccounts();
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- var smtpInfo = dataManager.GetSmtpInfo();
- if (smtpInfo != null)
- {
- SmtpServer.Text = smtpInfo.SmtpServer;
- SmtpAccount.Text = smtpInfo.SmtpAccount;
- SmtpPassword.Password = smtpInfo.SmtpPassword;
- }
- else
- {
- SmtpServer.Text = "host.vinno.com";
- SmtpAccount.Text = "ai@vinno.com";
- SmtpPassword.Password = "68FwL8XZE4";
- }
- }
- private MimeMessage CreateMessage(string from, string to, string title, string fromName, string toName, Stream reportStream)
- {
- var message = new MimeMessage();
- message.From.Add(new MailboxAddress(fromName, from));
- message.To.Add(new MailboxAddress(toName, to));
- message.Subject = title;
- // create our message text, just like before (except don't set it as the message.Body)
- var body = new TextPart("plain")
- {
- Text = $"{toName}, 您好,请查阅附件中有关您的标注审核报告,如有疑问,可以联系标注系统的负责人。此邮件为系统自动发出,请勿直接回复。"
- };
- // create an image attachment for the file located at path
- var attachment = new MimePart("application","pdf")
- {
- Content = new MimeContent(reportStream),
- ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
- ContentTransferEncoding = ContentEncoding.Base64,
- FileName = $"{DateTime.Now:yyyyMMdd}.pdf"
- };
- // now create the multipart/mixed container to hold the message text and the
- // image attachment
- var multipart = new Multipart("mixed") {body, attachment};
- // now set the multipart/mixed as the message body
- message.Body = multipart;
- return message;
- }
- private void SendBadReports()
- {
- if (Application.Current.MainWindow != null)
- {
- Application.Current.MainWindow.IsEnabled = false;
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- var startDate = (DateTime)StartDate.SelectedDate;
- var endDate = ((DateTime)EndDate.SelectedDate).AddDays(1);
- var smtpServer = SmtpServer.Text;
- var smtpAccount = SmtpAccount.Text;
- var smtpPassword = SmtpPassword.Password;
- Task.Run(() =>
- {
- try
- {
- LogManager.WriteLog("开始发送报告...");
- var accounts = _accountViewModels.Where(x => x.IsSelected).ToList();
- var accountIndex = 0;
- foreach (var account in accounts)
- {
- var committedImages =
- dataManager.GetReviewedBadCommittedImageCount(account.Id, startDate, endDate).ToList();
- if (committedImages.Count > 0)
- {
- using (var reportDocument = new ReportDocument(new PdfDocument()))
- {
- var coverPage = (CoverPage) reportDocument.CreateCoverPage();
- coverPage.Title =
- $"{startDate.ToLongDateString()}-{endDate.ToLongDateString()}标注错误审核报告";
- coverPage.UserName = account.FriendlyName;
- coverPage.Render();
- foreach (var committedImage in committedImages)
- {
- var bestCommittedImage = dataManager.GetBestCommittedImage(committedImage.ImageId);
- BitmapSource bestImage = null;
- var bestConclusion = new StringBuilder();
- if (bestCommittedImage != null)
- {
- var bestImageData = ImageData.FromXml(bestCommittedImage.Content);
- bestConclusion.AppendLine($"部位:{bestImageData.RootLabelTitle}");
- bestConclusion.AppendLine($"图像结论:{bestImageData.Conclusion?.Title}");
- bestConclusion.Append("ROI结论:");
- foreach (var imageRoi in bestImageData.Rois)
- {
- bestConclusion.Append(
- $"[ROI-{imageRoi.Index}] - {imageRoi.Conclusion?.Title}, ");
- }
- bestImage = BitmapFrame.Create(new MemoryStream(bestImageData.Data));
- var bestVisual = new DrawingVisual();
- var bestContext = bestVisual.RenderOpen();
- bestContext.DrawImage(bestImage, new Rect(0, 0, bestImage.Width, bestImage.Height));
- foreach (var imageRoi in bestImageData.Rois)
- {
- StreamGeometry geometry = new StreamGeometry
- {
- FillRule = FillRule.Nonzero
- };
- var points = imageRoi.Points.Select(x => new Point(x.X, x.Y)).ToArray();
- using (StreamGeometryContext ctx = geometry.Open())
- {
- ctx.BeginFigure(points[0], true, true);
- ctx.PolyLineTo(points, true, true);
- ctx.LineTo(points[0], true, true);
- }
- geometry.Freeze();
- bestContext.DrawGeometry(Brushes.Transparent, new Pen(Brushes.Lime, 3), geometry);
- var indexStr = new FormattedText(
- imageRoi.Index.ToString(),
- CultureInfo.GetCultureInfo("zh-cn"),
- FlowDirection.LeftToRight,
- new Typeface(new FontFamily("Microsoft YaHei"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
- 12,
- Brushes.White);
- var width = Math.Max(indexStr.Width, indexStr.Height) + 2;
- var height = Math.Max(indexStr.Width, indexStr.Height) + 2;
- var left = points.Min(x => x.X) - width;
- var top = points.Min(x => x.Y) - height;
- var rectTopLeft = new Point(left, top);
- bestContext.DrawRoundedRectangle(Brushes.DarkGoldenrod, new Pen(Brushes.Green, 0.5), new Rect(rectTopLeft, new Size(width, height)), 4, 4);
- var textTopLeft = new Point(left + (width - indexStr.Width) / 2, top + (height - indexStr.Height) / 2);
- bestContext.DrawText(indexStr, textTopLeft);
- }
- bestContext.Close();
- bestImage = new RenderTargetBitmap(bestImage.PixelWidth, bestImage.PixelHeight, 96, 96, PixelFormats.Default);
- ((RenderTargetBitmap)bestImage).Render(bestVisual);
- }
- var conclusion = new StringBuilder();
- var image = ImageData.FromXml(committedImage.Content);
- conclusion.AppendLine($"部位:{image.RootLabelTitle}");
- conclusion.AppendLine($"图像结论:{image.Conclusion?.Title}");
- conclusion.Append("ROI结论:");
- foreach (var imageRoi in image.Rois)
- {
- conclusion.Append(
- $"[ROI-{imageRoi.Index}] - {imageRoi.Conclusion?.Title}, ");
- }
- var originalImage = BitmapFrame.Create(new MemoryStream(image.Data));
- var visual = new DrawingVisual();
- var context = visual.RenderOpen();
- context.DrawImage(originalImage, new Rect(0,0, originalImage.Width, originalImage.Height));
- foreach (var imageRoi in image.Rois)
- {
- StreamGeometry geometry = new StreamGeometry
- {
- FillRule = FillRule.Nonzero
- };
- var points = imageRoi.Points.Select(x => new Point(x.X, x.Y)).ToArray();
- using (StreamGeometryContext ctx = geometry.Open())
- {
- ctx.BeginFigure(points[0], true, true);
- ctx.PolyLineTo(points, true, true);
- ctx.LineTo(points[0], true, true);
- }
- geometry.Freeze();
- context.DrawGeometry(Brushes.Transparent, new Pen(Brushes.Lime,3), geometry);
- var indexStr = new FormattedText(
- imageRoi.Index.ToString(),
- CultureInfo.GetCultureInfo("zh-cn"),
- FlowDirection.LeftToRight,
- new Typeface(new FontFamily("Microsoft YaHei"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
- 12,
- Brushes.White);
- var width = Math.Max(indexStr.Width, indexStr.Height) + 2;
- var height = Math.Max(indexStr.Width, indexStr.Height) + 2;
- var left = points.Min(x => x.X) - width;
- var top = points.Min(x => x.Y) - height;
- var rectTopLeft = new Point(left, top);
- context.DrawRoundedRectangle(Brushes.DarkGoldenrod, new Pen(Brushes.Green, 0.5), new Rect(rectTopLeft, new Size(width, height)), 4, 4);
- var textTopLeft = new Point(left + (width - indexStr.Width) / 2, top + (height - indexStr.Height) / 2);
- context.DrawText(indexStr, textTopLeft);
- }
- context.Close();
- var labeledImage = new RenderTargetBitmap(originalImage.PixelWidth, originalImage.PixelHeight,96,96, PixelFormats.Default);
- labeledImage.Render(visual);
- var contentPage = (ContentPage)reportDocument.CreateContentPage();
- contentPage.BestConclusion = bestConclusion.ToString();
- contentPage.BestImage = bestImage;
- contentPage.OriginalImage = originalImage;
- contentPage.LabeledImage = labeledImage;
- contentPage.Conclusion = conclusion.ToString();
- contentPage.ReviewDescription = committedImage.ResultDescription;
- contentPage.Render();
- }
- using (var reportStream = new MemoryStream())
- {
- reportDocument.Save(reportStream);
- //Send Email
- var message = CreateMessage(smtpAccount, account.Email, "AI图像标注审核报告",
- "AI标注系统", account.FriendlyName, reportStream);
- if (message != null)
- {
- using (var client = new SmtpClient())
- {
- client.ServerCertificateValidationCallback = (s, c, h, e) => true;
- client.Connect(smtpServer, 465, SecureSocketOptions.SslOnConnect);
- client.Authenticate(smtpAccount, smtpPassword);
- client.Send(message);
- client.Disconnect(true);
- }
- }
- }
- }
- }
- accountIndex++;
- var progress = (int)((float)accountIndex / accounts.Count * 100);
- LogManager.WriteLog($"报告发送中...{progress}%");
- }
- dataManager.SetSmtpInfo(smtpServer,smtpAccount,smtpPassword);
- }
- catch (Exception ex)
- {
- LogManager.WriteLog($"报告发送出错:{ex.Message}。");
- }
- finally
- {
- Dispatcher.Invoke(() => { Application.Current.MainWindow.IsEnabled = true; });
- }
- });
- }
- }
- private void CreateTotalBadReport(string filePath)
- {
- if (Application.Current.MainWindow != null)
- {
- Application.Current.MainWindow.IsEnabled = false;
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- var startDate = (DateTime)StartDate.SelectedDate;
- var endDate = ((DateTime)EndDate.SelectedDate).AddDays(1);
- Task.Run(() =>
- {
- using (var reportDocument = new ReportDocument(new PdfDocument()))
- {
- try
- {
- LogManager.WriteLog("开始生成报告...");
- var accounts = _accountViewModels.Where(x => x.IsSelected).ToList();
- var accountIndex = 0;
- foreach (var account in accounts)
- {
- var committedImages =
- dataManager.GetReviewedBadCommittedImageCount(account.Id, startDate, endDate)
- .ToList();
- if (committedImages.Count > 0)
- {
- var coverPage = (CoverPage) reportDocument.CreateCoverPage();
- coverPage.Title =
- $"{startDate.ToLongDateString()}-{endDate.ToLongDateString()}标注错误审核报告";
- coverPage.UserName = account.FriendlyName;
- coverPage.Render();
- foreach (var committedImage in committedImages)
- {
- var bestCommittedImage = dataManager.GetBestCommittedImage(committedImage.ImageId);
- BitmapSource bestImage = null;
- var bestConclusion = new StringBuilder();
- if (bestCommittedImage != null)
- {
- var bestImageData = ImageData.FromXml(bestCommittedImage.Content);
- bestConclusion.AppendLine($"部位:{bestImageData.RootLabelTitle}");
- bestConclusion.AppendLine($"图像结论:{bestImageData.Conclusion?.Title}");
- bestConclusion.Append("ROI结论:");
- foreach (var imageRoi in bestImageData.Rois)
- {
- bestConclusion.Append(
- $"[ROI-{imageRoi.Index}] - {imageRoi.Conclusion?.Title}, ");
- }
- bestImage = BitmapFrame.Create(new MemoryStream(bestImageData.Data));
- var bestVisual = new DrawingVisual();
- var bestContext = bestVisual.RenderOpen();
- bestContext.DrawImage(bestImage, new Rect(0, 0, bestImage.Width, bestImage.Height));
- foreach (var imageRoi in bestImageData.Rois)
- {
- StreamGeometry geometry = new StreamGeometry
- {
- FillRule = FillRule.Nonzero
- };
- var points = imageRoi.Points.Select(x => new Point(x.X, x.Y)).ToArray();
- using (StreamGeometryContext ctx = geometry.Open())
- {
- ctx.BeginFigure(points[0], true, true);
- ctx.PolyLineTo(points, true, true);
- ctx.LineTo(points[0], true, true);
- }
- geometry.Freeze();
- bestContext.DrawGeometry(Brushes.Transparent, new Pen(Brushes.Lime, 3), geometry);
- var indexStr = new FormattedText(
- imageRoi.Index.ToString(),
- CultureInfo.GetCultureInfo("zh-cn"),
- FlowDirection.LeftToRight,
- new Typeface(new FontFamily("Microsoft YaHei"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
- 12,
- Brushes.White);
- var width = Math.Max(indexStr.Width, indexStr.Height) + 2;
- var height = Math.Max(indexStr.Width, indexStr.Height) + 2;
- var left = points.Min(x => x.X) - width;
- var top = points.Min(x => x.Y) - height;
- var rectTopLeft = new Point(left, top);
- bestContext.DrawRoundedRectangle(Brushes.DarkGoldenrod, new Pen(Brushes.Green, 0.5), new Rect(rectTopLeft, new Size(width, height)), 4, 4);
- var textTopLeft = new Point(left + (width - indexStr.Width) / 2, top + (height - indexStr.Height) / 2);
- bestContext.DrawText(indexStr, textTopLeft);
- }
- bestContext.Close();
- bestImage = new RenderTargetBitmap(bestImage.PixelWidth, bestImage.PixelHeight, 96, 96, PixelFormats.Default);
- ((RenderTargetBitmap)bestImage).Render(bestVisual);
- }
- var conclusion = new StringBuilder();
- var image = ImageData.FromXml(committedImage.Content);
- conclusion.AppendLine($"部位:{image.RootLabelTitle}");
- conclusion.AppendLine($"图像结论:{image.Conclusion?.Title}");
- conclusion.Append("ROI结论:");
- foreach (var imageRoi in image.Rois)
- {
- conclusion.Append(
- $"[ROI-{imageRoi.Index}] - {imageRoi.Conclusion?.Title}, ");
- }
- var originalImage = BitmapFrame.Create(new MemoryStream(image.Data));
- var visual = new DrawingVisual();
- var context = visual.RenderOpen();
- context.DrawImage(originalImage, new Rect(0, 0, originalImage.Width, originalImage.Height));
- foreach (var imageRoi in image.Rois)
- {
- StreamGeometry geometry = new StreamGeometry
- {
- FillRule = FillRule.Nonzero
- };
- var points = imageRoi.Points.Select(x => new Point(x.X, x.Y)).ToArray();
- using (StreamGeometryContext ctx = geometry.Open())
- {
- ctx.BeginFigure(points[0], true, true);
- ctx.PolyLineTo(points, true, true);
- ctx.LineTo(points[0], true, true);
- }
- geometry.Freeze();
- context.DrawGeometry(Brushes.Transparent, new Pen(Brushes.Lime, 3), geometry);
- var indexStr = new FormattedText(
- imageRoi.Index.ToString(),
- CultureInfo.GetCultureInfo("zh-cn"),
- FlowDirection.LeftToRight,
- new Typeface(new FontFamily("Microsoft YaHei"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
- 12,
- Brushes.White);
- var width = Math.Max(indexStr.Width, indexStr.Height) + 2;
- var height = Math.Max(indexStr.Width, indexStr.Height) + 2;
- var left = points.Min(x => x.X) - width;
- var top = points.Min(x => x.Y) - height;
- var rectTopLeft = new Point(left, top);
- context.DrawRoundedRectangle(Brushes.DarkGoldenrod,new Pen(Brushes.Green,0.5), new Rect(rectTopLeft, new Size(width,height)),4,4 );
- var textTopLeft = new Point(left + (width - indexStr.Width)/2, top + (height - indexStr.Height)/2);
- context.DrawText(indexStr, textTopLeft);
- }
- context.Close();
- var labeledImage = new RenderTargetBitmap(originalImage.PixelWidth, originalImage.PixelHeight, 96, 96, PixelFormats.Default);
- labeledImage.Render(visual);
- var contentPage = (ContentPage)reportDocument.CreateContentPage();
- contentPage.BestConclusion = bestConclusion.ToString();
- contentPage.BestImage = bestImage;
- contentPage.OriginalImage = originalImage;
- contentPage.LabeledImage = labeledImage;
- contentPage.Conclusion = conclusion.ToString();
- contentPage.ReviewDescription = committedImage.ResultDescription;
- contentPage.Render();
- }
- }
- accountIndex++;
- var progress = (int) ((float) accountIndex / accounts.Count * 100);
- LogManager.WriteLog($"报告生成中...{progress}%");
- }
- reportDocument.Save(filePath);
- LogManager.WriteLog("报告生成成功。");
- }
- catch (Exception ex)
- {
- LogManager.WriteLog($"报告生成出错:{ex.Message}。");
- }
- finally
- {
- Dispatcher.Invoke(() => { Application.Current.MainWindow.IsEnabled = true; });
- }
- }
- });
- }
- }
- private void LoadAccounts()
- {
- AccountSelector.Text = string.Empty;
- foreach (var accountViewModel in _accountViewModels)
- {
- accountViewModel.IsSelectedChanged -= OnAccountViewModelIsSelectedChanged;
- }
- _accountViewModels.Clear();
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- var accounts = dataManager.GetAccounts();
- foreach (var account in accounts)
- {
- var accountViewModel = new AccountViewModel
- {
- Id = account.Id,
- Name = account.Name,
- Description = account.Description,
- Password = account.Password,
- CreatedTime = account.CreatedTime,
- Labels = account.Labels,
- IsReviewer = account.IsReviewer,
- Email = account.Email,
- };
- accountViewModel.IsSelectedChanged += OnAccountViewModelIsSelectedChanged;
- _accountViewModels.Add(accountViewModel);
- }
- AccountSelector.SelectAll();
- }
- private void OnAccountViewModelIsSelectedChanged(object sender, EventArgs e)
- {
- var selectedNames = _accountViewModels.Where(x => x.IsSelected).Select(x => x.FriendlyName).ToArray();
- AccountSelector.Text = string.Join(",", selectedNames);
- }
- private void OnClearClick(object sender, RoutedEventArgs e)
- {
- Result.Items.Clear();
- }
- private void OnSelectUnSelectAllAccounts(object sender, bool e)
- {
- foreach (var accountViewModel in _accountViewModels)
- {
- accountViewModel.IsSelected = e;
- }
- }
- private void OnAccountsOnRefreshed(object sender, EventArgs e)
- {
- LoadAccounts();
- }
- private void OnCalcReviewedClick(object sender, RoutedEventArgs e)
- {
- Result.Items.Clear();
- if (Application.Current.MainWindow != null)
- {
- Application.Current.MainWindow.IsEnabled = false;
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- var startDate = (DateTime)StartDate.SelectedDate;
- var endDate = ((DateTime)EndDate.SelectedDate).AddDays(1);
- var resultList = new List<ReviewInfo>();
- Task.Run(() =>
- {
- try
- {
- LogManager.WriteLog("开始统计信息...");
- var accounts = _accountViewModels.Where(x => x.IsSelected).ToList();
- var accountIndex = 0;
- foreach (var account in accounts)
- {
- var bestCount = dataManager.GetReviewedCommittedImageCount(account.Id,ReviewResult.Best,startDate, endDate);
- var okCount = dataManager.GetReviewedCommittedImageCount(account.Id, ReviewResult.Ok, startDate, endDate);
- var badCount = dataManager.GetReviewedCommittedImageCount(account.Id, ReviewResult.Fail, startDate, endDate);
- resultList.Add(new ReviewInfo(account.FriendlyName, bestCount, okCount, badCount));
- accountIndex++;
- var progress = (int)((float)accountIndex / accounts.Count * 100);
- LogManager.WriteLog($"统计信息中...{progress}%");
- }
- Dispatcher.Invoke(() =>
- {
- foreach (var result in resultList)
- {
- Result.Items.Add(result);
- }
- });
- LogManager.WriteLog("统计信息完毕。");
- }
- catch (Exception ex)
- {
- LogManager.WriteLog($"统计信息出错:{ex.Message}。");
- }
- finally
- {
- Dispatcher.Invoke(() => { Application.Current.MainWindow.IsEnabled = true; });
- }
- });
- }
- }
- private void OnExportTotalBadReportClick(object sender, RoutedEventArgs e)
- {
- Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
- sfd.Filter = "*.pdf|*.pdf";
- if (sfd.ShowDialog() == true)
- {
- CreateTotalBadReport(sfd.FileName);
- }
- }
- private void OnSendBadReportsClick(object sender, RoutedEventArgs e)
- {
- if (string.IsNullOrEmpty(SmtpServer.Text))
- {
- MessageBox.Show("请输入SMTP服务器地址", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- SmtpServer.Focus();
- return;
- }
- if (string.IsNullOrEmpty(SmtpAccount.Text))
- {
- MessageBox.Show("请输入发送邮件的账号", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- SmtpAccount.Focus();
- return;
- }
- if (string.IsNullOrEmpty(SmtpPassword.Password))
- {
- MessageBox.Show("请输入发送邮件账号的密码", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- SmtpPassword.Focus();
- return;
- }
- SendBadReports();
- }
- }
- }
|